How to set / limit Magnitude?

Hey guys i currently trying to make a SpeedLimiter but i stuck to limit the TopSpeed
This is my current code:

local CurrentUnitScaling = (10/12) * 1.09728
local player = game:GetService(“Players”).LocalPlayer
local pGui = player.PlayerGui
local Agui= pGui:WaitForChild(“A-Chassis Interface”)
local TopSpeed = 100

while true do
wait()
local VelocityValue = Agui.Values.Velocity.Value
local CurrentSpeed = math.floor(CurrentUnitScaling * VelocityValue.Magnitude)
if CurrentSpeed > TopSpeed then
VelocityValue.Magnitude = TopSpeed
end
end

Error code is: Magnitude cannot be asigned to.
Any other methode or is it even possible to limit it ?

Cars using A-Chassis.

Thanks for Help!

1 Like
velocity = velocity.Unit * math.min(velocity.Magnitude, topSpeed)

Is this one right now?
Because the VelocityValue still goes higher and the Car don’t limit the speed.

while true do
	wait()
	local VelocityValue = Agui.Values.Velocity.Value
	local CurrentSpeed = math.floor(CurrentUnitScaling * VelocityValue.Magnitude)
	if CurrentSpeed > TopSpeed then
		VelocityValue = VelocityValue.Unit * math.min(VelocityValue.Magnitude, TopSpeed)
	end
end

Does the if statement triggers?

yes the if statement it getting triggered

Multiple times?

This text will be blurred

Always when its getting over the TopSpeed so yeah

Unbenannt

Try this instead

VelocityValue = VelocityValue.Unit * TopSpeed

Nothing happens, the Value still goes Higher

Strange, in my testing it works perfectly.

So the Car has Velocity value from this Value the SpeedGauge gets the Current car speed and etc.

Ive tried this now to the with DriveSeat Position, DriveSeat Velocity, DriveSeat Velocity.Magnitude and the VelocityValue from the Car, all don’t work / all ignore it be like.

Ah wait i think i know why.

local VelocityValue = Agui.Values.Velocity.Value -- you're calling .Value, therefore it actually does not update the Velocity when you re-assign the variable.
local CurrentSpeed = math.floor(CurrentUnitScaling * VelocityValue.Magnitude)
if CurrentSpeed > TopSpeed then
    VelocityValue = VelocityValue.Unit * TopSpeed
end

Try this.

local VelocityValue = Agui.Values.Velocity
local CurrentSpeed = math.floor(CurrentUnitScaling * VelocityValue.Value.Magnitude)
if CurrentSpeed > TopSpeed then
    VelocityValue.Value = VelocityValue.Value.Unit * TopSpeed
end
1 Like

Still don’t work, im confused eee

You said its working for you, can i may see your script or how you did it ?

local vel = Vector3.new(1, 0, 0)
local acceleration = 10

game:GetService('RunService').Heartbeat:Connect(function(dt)
	if vel.Magnitude > 50 then
		vel = vel.Unit * 50
	end
	vel += Vector3.new(acceleration * dt, 0, 0)
	print(
		string.format('%.2f', vel.Magnitude)
	)
end)

I use “while true do” should i use RunService instead ?

Yeah, a client lag would speed down the while loop

Must also a local script right ?

I’m doing a server script for testing, you can use .RenderStepped instead of .Heartbeat for localscript

Aight, now its working, the problem was the “while true do loop”

Thanks for your help!

1 Like