When jumping off a vehicleseat the values always reset back to their original settings

Sorry this script is probably pretty messy, still learning and the tabs don’t appear in the post.
The issue I have is that there are 3 similar LocalScripts in my StarterCharacterScripts folder like the one below. Each controls particular kinds of vehicles in the Workspace; Dump Trucks, Bulldozers and Front-End Loaders. It’s a one player game (due to lag with many moving Parts in multi-player games.
Here’s the issue, in Studio when I jump off a VehicleSeat the controllable parts of my vehicles stay where they were left (ie: dump truck bed stays at its last position, loader controls stay at their positions, etc.) but when I play the game on the website I leave the seat and the controls go back to their original position and when I get back in the seat the parts go back to where they were before I jumped out the first time.
I’ve tried setting the values to nil so you don’t keep controlling the vehicle after you leave but that doesn’t help the problem, the vehicles still reset when you leave the seat and go back to their last settings when you touch the seat.
Like I said the other scripts are similar, they just reference different controls/welds/hingeconstraints/lights.
Why is this working great in Studio and not in game?

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild(“Humanoid”)
local UIS = game:GetService(“UserInputService”)
local dump, door, step, smoke --references to the current seat’s controls
local l1, l2, l3, l4, l5, r1, r2 --references light controls
local working --last pressed KeyCode

humanoid.Seated:connect(function(active, seat)
if active then
wait(.1)
working = false
if seat:FindFirstChild(“Attachment”) then – safe to assume it is a Dump Truck
–print(‘Player sat on Dump Truck seat.’)
dump = seat.Parent.Dump.Motor
dump.DesiredAngle = dump.CurrentAngle
door = seat.Parent.Door.HingeConstraint
step = seat.Parent.Parent.Steps.Step.HingeConstraint
smoke = seat.Parent.Smoke.ParticleEmitter
l1 = seat.Parent.Light1
l2 = seat.Parent.Light2
l3 = seat.Parent.Light3
l4 = seat.Parent.Light4
l5 = seat.Parent.Light5.SpotLight
r1 = seat.Parent.Red1
r2 = seat.Parent.Red2
door.TargetAngle = 0
step.TargetAngle = -165
wait(1)
smoke.Enabled = true
end
elseif dump then – test for dump just to be safe (i.e. if they got up off a non-dump truck seat, I don’t want the script to crash)
–print(‘Player left Dump Truck seat.’)
–reset and clear dump/door/step when they leave the seat
dump.DesiredAngle = dump.CurrentAngle
smoke.Enabled = false
l1.Material = “Ice”
l2.Material = “Ice”
l3.Material = “Ice”
l4.Material = “Ice”
l5.Enabled = false
r1.Material = “Slate”
r2.Material = “Slate”
door.TargetAngle = 50
step.TargetAngle = 0
print("Dump: " … dump.CurrentAngle)
–[[dump, door, step, smoke, l1, l2, l3, l4, l5, r1, r2, working = nil
tried making this active so the controls aren’t available after
getting off the seat but it resets the dump angle back to 0 ]]
end
end)

UIS.InputBegan:connect(function(InputObject,gameProcessedEvent)
if dump then – only do anything if they’re sitting in the dump truck seat
if UIS:IsKeyDown(Enum.KeyCode.R) then
–print"Truck dump up"
wait()
working = Enum.KeyCode.R
dump.DesiredAngle =1.1
if dump.DesiredAngle<0.001 then dump.DesiredAngle=0.01 end
elseif UIS:IsKeyDown(Enum.KeyCode.F)then
–print"Truck dump down"
working = Enum.KeyCode.F
wait()
dump.DesiredAngle = 0
elseif UIS:IsKeyDown(Enum.KeyCode.L)then
–print"Truck lights"
wait()
if l5.Enabled == true then
l1.Material = “Ice”
l2.Material = “Ice”
l3.Material = “Ice”
l4.Material = “Ice”
l5.Enabled = false
r1.Material = “Slate”
r2.Material = “Slate”
else
l1.Material = “Neon”
l2.Material = “Neon”
l3.Material = “Neon”
l4.Material = “Neon”
l5.Enabled = true
r1.Material = “Neon”
r2.Material = “Neon”
end
end
wait()
end
end)

UIS.InputEnded:connect(function(InputObject,gameProcessedEvent)
if dump then – only do anything if they’re sitting in the dump truck seat
if InputObject.KeyCode == working then
–print"Dump Truck input Stopped"
working = false
dump.DesiredAngle = dump.CurrentAngle
end
end
end)

uh, im sorry but its kinda hard to read a code like that for me

my suggestion is that u create a script on the vehicle, also add remotes

also u might wanna try coroutine.resume(coroutine.create(function() end))

Sorry 'bout that, was told by EchoReaper how to put 3 reverse italics on either side of code to show it correctly.

Working on a test place with easier code and simpler vehicles to show here.

Try using Motor6D’s instead of Motors, Motors typically replicate really poorly.

Another thing is to use Documentation - Roblox Creator Hub

Problem is everything works really well in Studio, but not when in-game when you jump out of a seat.
I looked into Network ownership and thought I may understand it but really I don’t.

Just let me get the test place up and running and I’ll post the whole thing here.

thats why i told u to use at least a sever script

Here’s a cleaned up version of Scottifly’s code, so it’s easier for people to read:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:wait()
local humanoid = character:WaitForChild(“Humanoid”)
local UIS = game:GetService(“UserInputService”)
local dump, door, step, smoke	--references to the current seat’s controls
local l1, l2, l3, l4, l5, r1, r2 --references light controls
local working	--last pressed KeyCode

humanoid.Seated:connect(function(active, seat)
  if active then
    wait(.1)
    working = false
    if seat:FindFirstChild(“Attachment”) then	– safe to assume it is a Dump Truck
      –print(‘Player sat on Dump Truck seat.’)
      dump = seat.Parent.Dump.Motor
      dump.DesiredAngle = dump.CurrentAngle
      door = seat.Parent.Door.HingeConstraint
      step = seat.Parent.Parent.Steps.Step.HingeConstraint
      smoke = seat.Parent.Smoke.ParticleEmitter
      l1 = seat.Parent.Light1
      l2 = seat.Parent.Light2
      l3 = seat.Parent.Light3
      l4 = seat.Parent.Light4
      l5 = seat.Parent.Light5.SpotLight
      r1 = seat.Parent.Red1
      r2 = seat.Parent.Red2
      door.TargetAngle = 0
      step.TargetAngle = -165
      wait(1)
      smoke.Enabled = true
    end
  elseif dump then	– test for dump just to be safe (i.e. if they got up off a non-dump truck seat, I don’t want the script to crash)
    –print(‘Player left Dump Truck seat.’)
    –reset and clear dump/door/step when they leave the seat
    dump.DesiredAngle = dump.CurrentAngle
    smoke.Enabled = false
    l1.Material = "Ice"
    l2.Material = "Ice"
    l3.Material = "Ice"
    l4.Material = "Ice"
    l5.Enabled = false
    r1.Material = "Slate"
    r2.Material = "Slate"
    door.TargetAngle = 50
    step.TargetAngle = 0
    print("Dump: " … dump.CurrentAngle)
    –[[dump, door, step, smoke, l1, l2, l3, l4, l5, r1, r2, working = nil
    tried making this active so the controls aren’t available after
    getting off the seat but it resets the dump angle back to 0 ]]
  end
end)

UIS.InputBegan:connect(function(InputObject,gameProcessedEvent)
  if dump then – only do anything if they’re sitting in the dump truck seat
    if UIS:IsKeyDown(Enum.KeyCode.R) then
      –print"Truck dump up"
      wait()
      working = Enum.KeyCode.R
      dump.DesiredAngle =1.1
      if dump.DesiredAngle<0.001 then dump.DesiredAngle=0.01 end
    elseif UIS:IsKeyDown(Enum.KeyCode.F)then
      –print"Truck dump down"
      working = Enum.KeyCode.F
      wait()
      dump.DesiredAngle = 0	
    elseif UIS:IsKeyDown(Enum.KeyCode.L)then
      –print"Truck lights"
      wait()
      if l5.Enabled == true then
        l1.Material = "Ice"
        l2.Material = "Ice"
        l3.Material = "Ice"
        l4.Material = "Ice"
        l5.Enabled = false
        r1.Material = "Slate"
        r2.Material = "Slate"
      else
        l1.Material = "Neon"
        l2.Material = "Neon"
        l3.Material = "Neon"
        l4.Material = "Neon"
        l5.Enabled = true
        r1.Material = "Neon"
        r2.Material = "Neon"
      end
    end
    wait()
  end
end)

UIS.InputEnded:connect(function(InputObject,gameProcessedEvent)
  if dump then – only do anything if they’re sitting in the dump truck seat
    if InputObject.KeyCode == working then
      –print"Dump Truck input Stopped"
      working = false	
      dump.DesiredAngle = dump.CurrentAngle
    end
  end
end)
5 Likes

There is a glitch where if you press R and F the working value is changed to F and it doesn’t line up if you stop pressing R. “if dump and (working == false) then”

And of course, VehicleSeat is a global object and switches network ownership whenever you jump out of it.

To understand Network Ownership, you have to understand server-to-client communication. Like if the server owns the part then the clients cannot change it. If a client owns the part (which happens whenever you sit in a vehicleSeat) then the client can change that part and it will replicate to the server. The server always replicates to the clients.