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)
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)
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)
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)
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)