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
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.
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.
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.
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.
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.
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?