Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Delay in Vehicle controls? {with code}

Asked by 5 years ago

Higher pings = Higher delay time in controls such as steering. Works perfectly fine and smooth in studio.

local seat = script.Parent.VehicleSeat
local engine = script.Parent.Body:WaitForChild("Engine")
local motors = script.Parent.Motors:GetChildren()
local servos = script.Parent.Servos



wait(1)
engine.Anchored = false

local speed = 0
---//Main 
while wait() do

    if seat.Throttle == 0 then
        if speed > 1 then
            speed = speed - 1
        elseif speed > 0 then
            speed = 0
        elseif speed < -1 then
            speed = speed + 1
        elseif speed < 0 then
            speed = 0
        end
    else
        speed = speed + seat.Throttle 
        speed = speed > seat.MaxSpeed and seat.MaxSpeed or speed < -seat.MaxSpeed and -seat.MaxSpeed or speed


    end
    for _, motor in pairs(motors) do
        motor.AngularVelocity = -seat.Throttle * 110
    end
    servos.Right.TargetAngle = -seat.Steer * 20
    servos.Left.TargetAngle = -seat.Steer * 20


end


local VehicleSeat = script.Parent:WaitForChild("VehicleSeat")

VehicleSeat.Changed:Connect(function(prop)
    if prop == "Occupant" then
        local humanoid = VehicleSeat.Occupant
        if humanoid then
            local player = game:GetService("Players"):GetPlayerFromCharacter(humanoid.Parent)
            if player then
                VehicleSeat:SetNetworkOwner(player) 
            end
        else
            VehicleSeat:SetNetworkOwnershipAuto()
        end
    end
end)

0
Everything located in a server-side script KhanPython 7 — 5y
0
Why are you abusing the while loop like that? User#24403 69 — 5y
0
Not sure if there are any other alternatives KhanPython 7 — 5y

1 answer

Log in to vote
0
Answered by
ozzyDrive 670 Moderation Voter
5 years ago

I'm assuming you're talking of special animations (such as tyre rotation) rather than the actual movement of the vehicle, as the movement should be fine considering the controller is the network owner of the vehicle -- unless you set otherwise.

In your current setup, the server is looking for user input. When a user presses the D key, the client updates the VehicleSeat's Steer property which is automatically replicated to the server -- yes, another undocumented behaviour but you had that figured out. This update has to be sent to the server, and as data takes time to travel, it also takes time for the server to receive this change.

The way you've set up your networking is quite bad.

The controlling client should be performing the animation itself, immediately when it notices that the user wants to steer. It should then send a status update to the server, telling whether it is turning left or right. The server can listen to changes within the Steer property if you don't want to go the remote route.

When the server then receives this status update, it should inform other clients of it rather than doing the animation itself (unless it's very important for the server to know the rotation of the servos). The other clients then do the animation themselves.

This is considered the proper way to do networking related to purely visual effects. You can also optimize performance -- what's the point of doing the animation if the client is nowhere near enough to see it?

0
I'm actually using ROBLOX's constraints in combination. No animations whatsoever KhanPython 7 — 5y
0
I wasn't referring to the literal animation objects, rather any movement of objects meant for only the visual effect. ozzyDrive 670 — 5y
Ad

Answer this question