Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

WHy does the :Destroy() function not work?

I have a script here:

tool = script.Parent
player = game.Players.LocalPlayer
mouse = player:GetMouse()
tool.Activated:connect(function()
	local Part = Instance.new("Part", workspace)
	Part.Size = Vector3.new(0.1,0.1,0.1)
	Part.Shape = ("Ball")
	Part.Position = Vector3.new(-3.5,0.5,4.5)
	Part.CFrame = tool.Handle.CFrame
	Part.CFrame = CFrame.new(Part.Position,mouse.Hit.p)
	local v = Instance.new("BodyVelocity", Part)
	v.velocity = Part.CFrame.lookVector *90
	v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
	wait(0.5)
	Part.Touched:connect(function(hit)

		if hit.Name == "Snow" then
			local Snow = hit
			Snow:Destroy()
		end	
	end)
	end)

But the problem is that when the projectile reaches the snow, nothing happens, and there is not even a error in the output.

So what's the problem and how do I fix it?

1 comment

0

Btw i recommend not going through the projectile script because i know it already works.

1 answer

✓ Accepted answer

Your code seems fine for the most part, but I think you're binding the .Touched() function too late. You use wait(0.5) on line 14. As the velocity of the part is 90 studs/second (as seen on line 12), this means the part will have traveled a minimum of 45 studs before the .Touched() event starts working on the part. I personally recommend just removing the wait(0.5).

tool = script.Parent
player = game.Players.LocalPlayer
mouse = player:GetMouse()

tool.Activated:connect(function()
	local Part = Instance.new("Part")
	Part.Size = Vector3.new(0.1,0.1,0.1)
	Part.Shape = ("Ball")
	Part.Position = Vector3.new(-3.5,0.5,4.5)
	Part.CFrame = CFrame.new(tool.Handle.Position,mouse.Hit.p)
	local v = Instance.new("BodyVelocity", Part)
	v.velocity = Part.CFrame.lookVector * 90
	v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
	Part.Parent = workspace
	Part.Touched:connect(function(hit)
		if hit.Name == "Snow" then
			hit:Destroy()
		end 
	end)
end)

Also, I don't recommend using the 2nd argument of Instance.new(obj,parent). It's been seen as causing a lot of unnecessary lag.

If this didn't solve your problem, feel free to tell me!

This discussion is preserved as it was. New answers, comments, and votes are closed.