this question is similar to the another, but im trying to make it so when you click a part, it deletes it

Viewed 17
while true do
    wait(1)
    local randomnumber = math.random(18,150)
    local x  = math.random(1,50)
    local Pointgiver = game.ReplicatedStorage.Points:Clone()
    Pointgiver.Parent = game.Workspace
    Pointgiver.Position = Vector3.new(randomnumber,0.5,x)
    Pointgiver.Transparency = 0
end
local pointgiver = game.Workspace:WaitForChild("Points")
pointgiver.ClickDetector.Mousetouch:Connect(function()
    pointgiver:Destroy()
end)

as you see, im using a waitforchild function which waits for a part to spawn and doesnt give errors thats like "There is no part named "Points" " but it wont destroy it, sorry if that didnt make sense

1 Answers

ClickDetector has no function named Mousetouch, instead, you should be using MouseClick which fires whenever the player clicks the respective Part. Also your code which listens for the ClickDetector.MouseClick is after the while loop and as such, it will never run as you keep calling the while loop repeatedly, and never break out of it.

while true do
    task.wait(1)

    local randomnumber = math.random(18,150)
    local x  = math.random(1,50)

    local Pointgiver = game.ReplicatedStorage.Points:Clone()
    Pointgiver.Position = Vector3.new(randomnumber,0.5,x)
    Pointgiver.Transparency = 0
    PointGiver.ClickDetector.MouseClick:Connect(function()
       PointGiver:Destroy()
    end)
    Pointgiver.Parent = game.Workspace
end
Related