Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 6 years ago

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.

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?

1 answer

Log in to vote
-1
Answered by 6 years ago

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
0
Wrong. FindFirstChild returns nil if specified object was not found, but I do agree about the ~= nil. It is redundant. User#19524 175 — 6y
0
i took the script off someone and that the totality of the script 20002000sa 83 — 6y
Ad