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

Applying maximum speed doesn't work?

Asked by 5 years ago

So I'm making a Model that is hovering on the ground (thanks to a BodyPosition that's Position is set a little above the ground and it's MaxForce is set to 0, 40000, 0) and can be steered. The model consists of a Part (in which is a Script, a BodyGyro, BodyPosition and a BodyVelocity) and a VehicleSeat that is welded to the Part. The Script makes it so the signals sent by the player to VehicleSeat are changing the properties of BodyGyro and BodyVelocity. This is how it looks:

local RunService = game:GetService("RunService")
local part = script.Parent
local bodyGyro = part.BodyGyro
local bodyVel = part.BodyVelocity
local seat = script.Parent.Parent.VehicleSeat
local goBack, goForward = false, false
local turnLeft, turnRight = false, false

local maxForward = 20
local maxBack = -10

seat:GetPropertyChangedSignal("Occupant"):Connect(function(char)
    local currentSpeed = 0
    local currentAngle = 0
    RunService.Heartbeat:Connect(function(step)
        bodyVel.Velocity = (part.CFrame.LookVector  * currentSpeed)
        bodyGyro.CFrame =  CFrame.Angles(math.rad(0), math.rad(currentAngle), math.rad(0))
        if seat.Throttle == -1 then
            if goBack then return end
            goBack = true
            repeat wait(.1)
                currentSpeed = currentSpeed - 1
            until seat.Throttle ~= -1 or currentSpeed <= maxBack -- The 2nd statement doesn't seem to do anything.
            goBack = false
        elseif seat.Throttle == 1 then
            if goForward then return end
            goForward = true
            repeat wait(.1)
                currentSpeed = currentSpeed + 1
            until seat.Throttle ~= 1 or currentSpeed >= maxForward -- Here too.
            goForward = false
        end
        if seat.Steer == -1 then
            if turnLeft then return end
            turnLeft = true
            repeat wait(.01) 
                currentAngle = currentAngle + 1
            until seat.Steer ~= -1   
            turnLeft = false              
        elseif seat.Steer == 1 then 
            if turnRight then return end
            turnRight = true
            repeat wait(.01) 
                currentAngle = currentAngle - 1
            until seat.Steer ~= 1
            turnRight = false
        end
    end)
end)

The Model speeds up fine and rotates fine too, but the maximum speed I applied to it doesn't do anything. The speed easily goes above 20. I tried doing other things to fix it but nothing worked. Thanks for reading.

0
I'm not too familiar with using forces, but have you tried setting max speed to much lower values? Other then that I could see using heartbeat causing a railgun effect since it's run so often, try putting a /60 after currentSpeed on line 16 maybe clc02 553 — 5y
0
It just makes the Model speed up slower Tymberlejk 143 — 5y

1 answer

Log in to vote
2
Answered by 5 years ago
local RunService = game:GetService("RunService")
local part = script.Parent
local bodyGyro = part.BodyGyro
local bodyVel = part.BodyVelocity
local seat = script.Parent.Parent.VehicleSeat
local goBack, goForward = false, false
local turnLeft, turnRight = false, false

local maxForward = 20
local maxBack = -10


local waitTime = 0.1--bigger it gets, slower boat will be
seat:GetPropertyChangedSignal("Occupant"):Connect(function(char)
    local currentSpeed = 0
    local currentAngle = 0
    RunService.Heartbeat:Connect(function(step)
        bodyVel.Velocity = (part.CFrame.LookVector  * currentSpeed)
        bodyGyro.CFrame =  CFrame.Angles(math.rad(0), math.rad(currentAngle), math.rad(0))
        if seat.Throttle == -1 then
--          if goBack then return end
 --           goBack = true--

            if seat.Throttle == -1 and (currentSpeed <= maxBack) then-- this
                currentSpeed = currentSpeed - 1
            end --

--          goBack = false
        elseif seat.Throttle == 1 then
 --         if goForward then return end
 --         goForward = true

            if seat.Throttle == 1 and (currentSpeed <= maxForward) then --and this
                currentSpeed = currentSpeed + 1
            end 

 --         goForward = false
        end
        if seat.Steer == -1 then
--          if turnLeft then return end
--          turnLeft = true

            if seat.Steer == -1 then 
                currentAngle = currentAngle + 1
            end    

--          turnLeft = false              
        elseif seat.Steer == 1 then 
--          if turnRight then return end
--          turnRight = true

            if seat.Steer == 1 then
                currentAngle = currentAngle - 1
            end

--            turnRight = false
        end
    end)
    wait(waitTime)
end)

I believe this should work, but I'm not sure why you need those booleans and checking them for nothing at 1, 2, 3 and 4 and then reassigning them you aren't using them, so to keep Code clean I recommend you to put them out

Ad

Answer this question