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

Can anyone help me with the spikes in my game? It keeps freezing.

In my game, i have spikes that kill you when you touch them with this script inserted inside:

function touch(hit)
	if hit.Parent:findFirstChild("Humanoid")~=nil then
		hit.Parent:findFirstChild("Humanoid").Health=0
	end
end
script.Parent.touched:connect(touch)

But when i press play, studio freezes and this message appears: "A script has been running for a long time, and may be hanging." This didn't happen before, it only started now. Note: i know this script is making the error and when studio freezes, i have to force quit it.

1 answer

I do not know the rest of your script so I can only answer a small amount based on what you have shown. I think the problem is that you are checking if hit.Parent:FindFirstChild("Humanoid") ~= nil. hit.Parent:FindFirstChild("Humanoid") returns false if the humanoid cannot be found. Because you are seeing if it is not nil it will always pass as true when it is false because false ~= nil. That means that this is firing whenever it touches something even if it does not have a humanoid which can cause a lot of problems. Just change it to say:

if hit.Parent:FindFirstChild("Humanoid") then

2 comments

0

Wrong. FindFirstChild returns nil if specified object was not found, but I do agree about the ~= nil. It is redundant.

0

i took the script off someone and that the totality of the script

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