How do I get the player from a vehicleseat?

I am trying to make a speed camera system.
It works if I set the event fire to FireAllClients, but I want it to be fired only if the localplayer is the driver thats on the vehicle seat
Drive is the name of the vehicleseat

Players = game:GetService("Players")
local debounce = false
speed = script.Parent.Parent.Light.MaxSpeed.Value
light = script.Parent.Parent.Light
function onTouched(part)
if debounce == false then
if part.Name == "Drive" and part.Velocity.magnitude > speed then
	if part.Occupant.Parent == Players.LocalPlayer then
	light.SpotLight.Enabled = true
	light.Material = Enum.Material.Neon
	game.ReplicatedStorage.PostEvents.TrafficCitation:FireClient(1,player)
	wait(.1)
    light.SpotLight.Enabled = false
	light.Material = Enum.Material.SmoothPlastic
	debounce = true
	print("Speeding")
	wait(.1)
	debounce = false
	end
	end
end
 end

 script.Parent.Touched:connect(onTouched)
1 Like

I think theres a property, its .Seated or something I think and itll have the player as a object

You’re using Occupant the wrong way. You should consult documentation before posting threads. Occupant is a reference to the Humanoid that’s on the seat. Instead of checking if the parent of Occupant is the LocalPlayer, use GetPlayerFromCharacter on the parent of Occupant and check if the returned player (if non-nil) is the LocalPlayer.

The reverse way of checking this is the SeatPart property of the Humanoid and checking if it’s the VehicleSeat. Either way works. Personally, I would go with SeatPart over Occupant.

3 Likes

It gives me an error “Unable to cast value to Object”
local Players = game:GetService(“Players”)
local debounce = false
speed = script.Parent.Parent.Light.MaxSpeed.Value
light = script.Parent.Parent.Light
function onTouched(part)
if debounce == false then
if part.Name == “Drive” and part.Velocity.magnitude > speed then
local player = Players:GetPlayerFromCharacter(part.Occupant.Parent)
light.SpotLight.Enabled = true
light.Material = Enum.Material.Neon
game.ReplicatedStorage.PostEvents.TrafficCitation:FireClient(1,player)
wait(.1)
light.SpotLight.Enabled = false
light.Material = Enum.Material.SmoothPlastic
debounce = true
print(“Speeding”)
wait(.1)
debounce = false
end
end
end

script.Parent.Touched:connect(onTouched)

Please format your code properly into a code block, indicate what line is experiencing the error and what code is at that line. I’m sure it has nothing to do with the GetPlayerFromCharacter call. Something that instantly caught my eye as a potential problem is this:

The first argument of FireClient needs to be a player instance, the client who should be receiving the event, not another value. It’s expecting an object and you cast a value here. I’d assume this is the problem and not anything related to what I just suggested you do.

1 Like

Yes. Thanks! You solved the problem! I was so dumb.

script.Parent:GetPropertyChangedSignal(“Occupant”):Connect(function()

end)