"Attempt to index nil with 'Parent' "

You can write your topic however you want, but you need to answer these questions:
I want the script to get to the player’s parent

  1. What is the issue?
    I’m trying to make a local for player, the script should detect the player when you sit in the seat, But i get this error that says "Attempt to index nil with ‘Parent’ " and I don’t see any issue with this so I’m trying to look for a fix

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Well, I’m new to scripting so i don’t know much about this error.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local par = script.Parent.Parent
local Load = script.Parent.headL:Clone()
local Anyone = script.Parent.Parent.Occupant
local smokes = script.Parent.Parent.Parent.Parent.Parts.Mesh
local player = game.Players:GetPlayerFromCharacter(Anyone.Parent) -- The error source
Load.Parent = player.Character
Load.Disabled = false
Load.light1.Value = smokes.lights.light1
Load.light2.Value = smokes.lights.light2
Load.light3.Value = smokes.lights.Clight
Load.Sound.Value = smokes.LightUse

par.r1.OnServerEvent:Connect(function(plr)
	script.light1.Value:Play()
	script.light1.Value.Transparency = 0
	script.light1.Value.headG.Enabled = true
	script.light2.Value.Transparency = 0
	script.light2.Value.headG.Enabled = true
	script.light3.Value.Transparency = 0
end)

par.r2.OnServerEvent:Connect(function(plr)
	script.light1.Value.Transparency = 1
	script.light1.Value.headG.Enabled = false
	script.light2.Value.Transparency = 1
	script.light2.Value.headG.Enabled = false
	script.light3.Value.Transparency = 1
end)

par.r3.OnServerEvent:Connect(function(plr)
	script.Sound.Value:Play()
	script.light1.Value.Transparency = 0
	script.light1.Value.headG.Enabled = true
	script.light2.Value.Transparency = 0
	script.light2.Value.headG.Enabled = true
	script.light3.Value.Transparency = 0
end)

par.r4.OnServerEvent:Connect(function(plr)
	script.light1.Value.Transparency = 1
	script.light1.Value.headG.Enabled = false
	script.light2.Value.Transparency = 1
	script.light2.Value.headG.Enabled = false
	script.light3.Value.Transparency = 1
end)

I mentioned where the error source is in the code. It’s in the fifth line.

Is it because this runs right away but your trying to find “Anyone.Parent” but if I am correct due to the Anyone looking for the Occupant but there is no one sitting it is equals to nil so that is why u have the issue while trying to find the parent of nothing.

To fix this issue you would need to detect when the user sits on the seat and hen runs this code. This can be done a few different ways and there are a few posts on the dev form on how you can detect if someone sits down.

Hey sorry for the late reply, It worked, thanks alot :slight_smile:

1 Like