✓ Accepted answer
Because the Touched event fires whenever ANY physical object touched it (even ones that are not player models). And if that's true, we know most things in a game don't have humanoids in them. So when you're using findfirstchild for a Humanoid, and one doesn't exist, it will return a nil value. Then you're saying .Sit = true, which will cause an error since the script reads it as:
nil.Sit = true -- (since findfirstchild returned nil)
And then you'll get the infamous error message of: "Error, attempt to index global / local 'Player' (a nil value)"
We can easily fix this by adding some flow control to our function, based on the condition that the Humanoid actually exists before trying to change something that may not exist, like this:
script.Parent.Touched:connect(function(hit)
local Player = hit.Parent:FindFirstChild("Humanoid")
if Player then
Player.Sit = true
end
end)
Hope it helped.
1 comment
No need to search for a humanoid inside a humanoid, you already referenced it.