Need help with script

I need help with this script I made (I’m new to scripting). I’m trying to make the script (the script is in a tool) destroy a part if it’s named Stone but it’s just destroying any part regardless of it’s name.

script.Parent.Handle.Touched:Connect(function(hit)
	if hit:FindFirstChild("Humanoid") == nil then
		if hit ~= nil then
			hit.Touched:Connect(game.Workspace.Stone)
			hit:Destroy()
		end
	end
end)

Try this

script.Parent.Handle.Touched:Connect(function(hit)
	if hit:FindFirstChild("Humanoid") == nil then
		if hit ~= nil then
			if hit.name = ("Stone") then
                          hit:Destroy()
end
			
		end
	end
end)

Sorry for messy code, i’m new to the formatting

1 Like

You don’t need to connect it just like you did on line 4. All you need to do is to check if the Hit exists and if it has a Parent, If so, We can just check it’s name and then Destroy it.

script.Parent.Handle.Touched:Connect(function(Hit)
	if Hit and Hit.Parent then
		if Hit.Name == "Stone" then
			Hit:Destroy()
			Hit = nil
		end
	end
end)

Whats the point of setting Hit to nil?
when you Destroy an instance it already consider as nil.

Mostly for Garbage Collection and avoid keeping it as a reference? I always used it so it just became a habit.

1 Like