Vehicle Seat Occupant only keys?

I am trying to make a boat for my game. i have all of the main controls (forward, left, right, backwards) in a global script but i also need to make some extra keys for things such as anchor. how would this be done?

i have made a localscript inside the seat but when i check for the occupant with

script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function()
    if script.Parent.Occupant == game.Players.LocalPlayer.Character.Humanoid then
	    print("test")
    end
end)

it doesn’t work.
what should i do?

3 Likes

You should use a remote event. Local scripts don’t affect the server.

Try using this instead:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()--ensuring character is loaded in
script.Parent:GetPropertyChangedSignal("Occupant"):Connect(function(Humanoid)
    if Humanoid and Humanoid.Parent == Character then
        --do stuff
    end
end)

did i ever say i wasn’t going to use remote events?

and @Romeo_Duncan that didn’t work. ill see if it if getting the signal

it isn’t getting the signal for the humanoid either

ensure the scripts parent is the seat and not the boat model

it is inside of the seat as it has been the entire time, still nothing

Well there is nothing wrong with the script i give you did you try checking if the signal fires? try printing a message

I’m sorry, try using his script but without the humanoid, just include the humanoid.character part

it isn’t firing @Romeo_Duncan idk what is wrong with it

ill test it out in studio now and get back to u

I just read on devhub that this property doesnt replicate so the change will only be detected from a server script so you have to do this code from the server,alternatively you can detect when the local player is sitting and check if the seat is equal to the vehicle seat like this:

local Player = game.Player.LocalPlayer
local Seat = script.Parent
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
--this function fires when the player sits or gets up
Humanoid.Seated:Connect(function(isSeated,seat)
      if isSeated and seat == Seat then
      --player is seated in boat
      end
end)
3 Likes

ah, ok. i will test this out and give you the feedback in a bit

it still isnt detecting it. i am going to try making it humanoid.Changed:Connect()

edit: nope

fixed it. only problem is. my camera is messed up when i get out of the seat

local Player = game.Players.LocalPlayer
local Character = Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.Seated:Connect(function(seated, seat)
if seated then
	local SeatParent = seat.Parent
	if seat.Name == "BoatSeat" then
		game:GetService("RunService").RenderStepped:Connect(function()
			if Humanoid.Seated then
				if seat.Name == "BoatSeat" then
					workspace.CurrentCamera.CameraType = Enum.CameraType.Scriptable
					workspace.CurrentCamera.CFrame = SeatParent.Camera.CFrame
				elseif not seat.Name == "BoatSeat" then
					workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
					workspace.CurrentCamera.CFrame = Character.Head.CFrame
				end
			else
				workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
				workspace.CurrentCamera.CFrame = Character.Head.CFrame
    			end
    		end)
    	end
    end
end)

The way you’re handling the camera is incorrect. Leave the camera setting to the default camera control module. Using a CameraType of Scriptable will automatically stop it from placing the camera in its intended place. You only need to update the Camera CFrame while the player is in the boat.

Something else: I think it would do you justice to use BindToRenderStep in this case so you can quickly bind and unbind the camera setting function. In your case, you spawn up a new connection to RenderStepped every time a player sits. That’s going to be really bad in the long run.

I’ve made a couple other improvements to your original code, such as removing unnecessary and redundant chunks. There are other improvements to be made (such as not assuming non-nils for your variables), but that’s something to fill out on your own time.

-- Remember: if this is in StarterPlayerScripts, you need to account for
-- respawns by updating variables upon respawn.
local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera

Humanoid.Seated:Connect(function (seated, seat)
    if seated and seat.Name == "BoatSeat" then
        local seatParent = seat.Parent
        Camera.CameraType = Enum.CameraType.Scriptable
        RunService:BindToRenderStep("BoatCamera", Enum.RenderPriority.Camera.Value, function()
            Camera.CFrame = seatParent.CFrame
        end)
    else
        -- Let camera module take over: do not set CFrame yourself
        Camera.CameraType = Enum.CameraType.Custom
        -- May have to do better handling regarding this bit, I just put this here as a catch-all case
        RunService:UnbindFromRenderStep("BoatCamera")
    end
end)
2 Likes

Adding to what @colbert2677 you can try saving the Cframe of the camera before you alter it like this:, I am not certain if this will work but you can give it try

local RunService = game:GetService("RunService")
local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera
local LastCFrame = nil

Humanoid.Seated:Connect(function (seated, seat)
    if seated and seat.Name == "BoatSeat" then
        local seatParent = seat.Parent
        LastCFrame = Camera.CFrame
        Camera.CameraType = Enum.CameraType.Scriptable
        RunService:BindToRenderStep("BoatCamera", Enum.RenderPriority.Camera.Value, function()
            Camera.CFrame = seatParent.CFrame
        end)
    else
       if LastCFrame then
          Camera.CFrame = LastCFrame
          LastCFrame = nil
       end
       Camera.CameraType = Enum.CameraType.Custom
       RunService:UnbindFromRenderStep("BoatCamera")
    end
end)
1 Like