Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I detect a player in a seat then give it a function?

I placed a LocalScript inside a VehicleSeat, and what I'm trying to do is that when the player sits, it can execute the following function.

local Seat =  script.Parent -- Path to seat
local LightPart = script.Parent.Parent.LightParts
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local on = false
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    if Seat.Occupant ~= nil then
        Mouse.KeyDown:connect(function(key)
            if key == "l" then
                if on == true then
                    on = false
                    LightPart.HeadLightPart.Material = Enum.Material.SmoothPlastic
                    LightPart.HeadLightPart2.Material = Enum.Material.SmoothPlastic
                else
                    LightPart.HeadLightPart.Material = Enum.Material.Neon
                    LightPart.HeadLightPart2.Material = Enum.Material.Neon
                end
            end
        end)
    end
end)

1 answer

✓ Accepted answer

There's a function to check if something is changed:

seat.Changed:Connect(function(ch)
	--ch is the property that changed
end)

And you can check if the property is the Occupant

seat.Changed:Connect(function(ch)
	if ch == "Occupant" then
		if seat.Occupant then
			local player = game.Players:FindFirstChild(seat.Occupant.Parent.Name) --The occupant is the humanoid, the humanoid.Parent is the player's character

			--Your code here

		end
	end
end)

Let me know if I helped

Edit: If you want to use a LocalScript, then here's an article about Humanoid.Seated

game.Players.LocalPlayer.Character.Humanoid.Seated:Connect(function(it)

	print(it) --This will print true or false

end)

12 comments

0

Are you sure that you tried it out?

0

Yes I did. Prints out an error?

0

It didn't work at all

0

Did it print out an error?

0

No, it didn't, but it didn't work at all

0

Try to put a print() after every line, and check if the code even runs

0

it can be placed inside of a localscript of a VehicleSeat, right?

0

I put a print() after every line

0

No it can't, use a ServerScript

0

wdym ServerScript? Do you mean ServerScriptService?

0

No, normal script

0

And I edited my answer, Iposted a LocalScript version of it

This discussion is preserved as it was. New answers, comments, and votes are closed.