Attempt to index nil with 'Changed'

This is code is contained in a script, which is parented to a seat:

local players = game:GetService("Players")
script.Parent.Occupant.Changed:Connect(function ()
	print(script.Parent.Occupant)
end)

However, when I test the place, I get this error:
Workspace.Model.Seat.Script:2: attempt to index nil with 'Changed'
Why does this happen, and how do I fix it?

The error attempt to index nil with 'Changed' means you’re trying to access to an event from an instance that doesn’t exist. Make sure Occupant exists in the parent of your script.

If my explanation doesn’t solve your problem, send a screenshot of your explorer and more details.

Here’s a screenshot
image

This script should fix your problem:

local players = game:GetService("Players")
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
    local Humanoid = script.Parent.Occupant
    if Humanoid then
        -- There's a player sitting
        print(Humanoid)
    end
end)
1 Like

That works, thanks!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.