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.
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
Closed as Not Constructive by User#19524
This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.
Why was this question closed?