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

I can't fix weird error in output. This has never happened to me before, can someone help?

Asked by 3 years ago
Edited 3 years ago

So I made a sitpart that when touched the player gets sit even if they don't want to be sitted. Here is the script:

script.Parent.Touched:Connect(function(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")
    Humanoid.Sit = true
end)

It works fine for me but when I look at the output this error appears: |23:32:20.221 Workspace.SitPart.Script3: attempt to index nil with 'Sit' - Server - Script:3

I have tried fixing it but I can't fix it. I have also tried adding a debounce to the script which wasn't successful because that same problem up there appeared in the output so I think that blocked the debounce from working.

0
"Attempt to index nil with ..." means that the Instance of the property you're trying to access doesn't exist. Ziffixture 6913 — 3y
0
Okay, thanks! Nitrolux200 62 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago

The problem is that the part being used with the touched event is also touching part(s) that don't have Humanoid within their parent. Try this out:

script.Parent.Touched:Connect(function(hit)
    local Humanoid = hit.Parent:FindFirstChild("Humanoid")
    if Humanoid then --Checks to see if a Humanoid is actually present
        Humanoid.Sit = true
    end
end)
0
Tysm! Nitrolux200 62 — 3y
Ad

Answer this question