Need help with changing string value when changing gear

Hello, I need some help with this code

What this code is supposed to do is to change the value of a string (in this case, the speed) depending on the gear it is on.

  • If it is on R then the value is supposed to be at 15
  • If it is N then it is 0
  • If it is on D1 then it is supposed to be at 8

Problem?
The R and N gears work but for D1 it doesn’t.

local connection = script.Parent.Parent.Parent["BUS CONTROLS"].boost.Value.Value
local gear = script.Parent.Parent.gear

while wait(0.5) do
	if  gear.Value == "R" then
		script.Parent.Value = 15
	end
	if  gear.Value == "N" then
		script.Parent.Value = 0
	end
	if  gear.Value == "D1" then
		script.Parent.Value = 8
	end
end
1 Like

2 Likes

I have no idea of why it doesn’t change the value, but I see one thing you could optimize: instead of a while wait loop, you could check for whenever the gear value changes with .Changed.

gear.Changed:Connect(function(NewValue)
--your code to detect what mode the bus is on
end)

This way you could also check whether it even detects what the value is if it’s set to D1 by printing NewValue.

1 Like

It seems like the issue might be related to the fact that the while loop is continuously running, and once it sets the value for “D1,” it immediately gets overridden by the next iteration.

local connection = script.Parent.Parent.Parent["BUS CONTROLS"].boost.Value.Value
local gear = script.Parent.Parent.gear

local function updateSpeed()
    if gear.Value == "R" then
        script.Parent.Value = 15
    elseif gear.Value == "N" then
        script.Parent.Value = 0
    elseif gear.Value == "D1" then
        script.Parent.Value = 8
    end
end

gear:GetPropertyChangedSignal("Value"):Connect(updateSpeed)

And i suggest you instead of using a continuous loop with wait(0.5) , you can use events or signals to trigger the script only when the gear value changes.

1 Like

A better approach is to use a ValueChanged event to trigger the code when the gear value changes. Additionally, to avoid unnecessary CPU usage, you can use an if-elseif structure instead of multiple if statements.

Script:

local boostValue = script.Parent.Parent.Parent["BUS CONTROLS"].boost.Value
local gear = script.Parent.Parent.gear
local speedValue = script.Parent

local function updateSpeed()
    if gear.Value == "R" then
        speedValue.Value = 15
    elseif gear.Value == "N" then
        speedValue.Value = 0
    elseif gear.Value == "D1" then
        speedValue.Value = 8
    end
end

gear.ValueChanged:Connect(updateSpeed)

updateSpeed()

This code connects the updateSpeed function to the ValueChanged event of the gear value. This way, the speed will be updated whenever the gear changes. The updateSpeed function is also called once initially to set the initial speed based on the initial gear value.

1 Like

Try this :

local speed = script.Parent
local gear = speed.Parent.Gear

local gearSpeedHolder = {["R"] = 15, ["N"] = 0, ["D1"] = "8",} -- Storing speed for gear letters to access for function easily

local function ConvertGearToSpeed(gearLetter)
	if not gearLetter then gearLetter = gear.Value end

	speed.Value = gearSpeedHolder[tostring(gearLetter)] or 0 -- set it to the speed from the table accordingly and if we don't find the gearLetter InTable let's set the speed to 0
end

gear.Changed:Connect(ConvertGearToSpeed)
ConvertGearToSpeed()

If it still doesn’t work then, It probably not this script you have to worry about but something else.

1 Like
local speed = script.Parent
local gear = speed.Parent.Gear

local gearSpeedHolder = { R = 15, N = 0, D1 = 8 }

local function ConvertGearToSpeed(gearLetter)
    gearLetter = gearLetter or gear.Value
    speed.Value = gearSpeedHolder[gearLetter] or 0
end

gear.Changed:Connect(ConvertGearToSpeed)
ConvertGearToSpeed()

In this version, I removed unnecessary quotes around gear letters in the gearSpeedHolder table and made the function slightly more concise. The default value for gearLetter is set using the or operator, which is a common idiom in Lua for handling optional parameters or nil values.

Hello guys, thanks for your help.

Despite several others giving me help and some new code, it still doesn’t work.

Doesn’t it work? The speed value is being change from 0, 8, and 15

Are you sure you aren’t changing the speed value elsewhere when setting it to D1? Because the code works for me just fine when testing.

Try printing the values of speed when they change to see if it ever prints 8 :

Speed.Changed:Connect(function(val)
   Print(val)
end)

Screenshot 2023-12-14 170945

Ok, I found the problem, it switches back to 0 after shifting to D1.
I don’t get why this happens.

This is probably because you are setting it to 0 somewhere else when the gear value changes to D1. What’s your gear script looks like? Is it possible to see it?

Ok, I finally found the issue and fixed it.

It turns out it was a poorly scripted anti-reverse that prevented it from changing speeds.
image

Thanks, guys for your help I appreciate it.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.