Seat.Occupant.Parent:GetPlayerFromCharacter()

Hi there, im currently trying to make a seat that has a function that only works for the player sitting in the seat, heres an image of what its for:

Picture:

This is for only the person in the seat to close the lapbar and no one else.

the code below here shows what im trying to do.

My code:

local humanoid = script.Parent.Parent.Parent.Seat.Occupant
local character = humanoid.Parent
local player = game.Players:GetPlayerFromCharacter(character)

script.Parent.MouseClick:Connect(function(plr)
	if plr.Name == player then
		script.Parent.Parent.Parent.Down.Transparency=0
		script.Parent.Parent.Transparency=1
		script.Parent.Parent.Sound:Play()
		script.Parent.MaxActivationDistance=0
	end
end)

After that I tested it out and brought back this error:

Seems to be this line here:

Does anyone know the issue?

1 Like
local Seat =  script.Parent.Parent.Parent.Seat -- Path to seat
local character
local player
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant ~= nil then
		character = Seat.Occupant.Parent
        player = game.Players:GetPlayerFromCharacter(character)
	end
end)
script.Parent.MouseClick:Connect(function(plr)
	if player and plr.Name == player then
		script.Parent.Parent.Parent.Down.Transparency=0
		script.Parent.Parent.Transparency=1
		script.Parent.Parent.Sound:Play()
		script.Parent.MaxActivationDistance=0
	end
end
1 Like

It can be that this line of code:

local humanoid = script.Parent.Parent.Parent.Seat.Occupant

Can have too much .Parents in it.
Can you send an image of what Occupant is in?

It didn’t work but gave no errors back in output.

image

local Seat =  script.Parent.Parent.Parent.Seat -- Path to seat
local character
local player
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()

	character = Seat.Occupant.Parent
    player = game.Players:GetPlayerFromCharacter(character)
end)
script.Parent.MouseClick:Connect(function(plr)
	if player and plr.Name == player.Name then
		script.Parent.Parent.Parent.Down.Transparency=0
		script.Parent.Parent.Transparency=1
		script.Parent.Parent.Sound:Play()
		script.Parent.MaxActivationDistance=0
	end
end

I think I found it out try this:

local humanoid = script.Parent.Parent.Parent.Seat.Occupant
local character = humanoid.Parent
local player = game.Players:GetPlayerFromCharacter(character)
script.Parent.MouseClick:Connect(function(plr)
if plr.Name == player.Name then
script.Parent.Parent.Parent.Down.Transparency=0
script.Parent.Parent.Transparency=1
script.Parent.Parent.Sound:Play()
script.Parent.MaxActivationDistance=0
end
end)

this worked, i have been trying so long and you finally got it, thank you!

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