Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

While loop speeding up with each input change?

So I'm trying to make a script that takes the value from one IntValue (We'll call it Value A), process it, and apply it to a different IntValue (Value B). So far the code works, but I have a problem with it speeding up.

As an example, the first time it reads Value A, Value B will increase/decrease by 1 every 0.1 seconds. The second time it reads from Value A, Value B changes every 0.05 seconds (not accurate, but just to give an idea of what's happening).

bin = script.Parent
Thrust = bin.Thrust --This is Value A, where it reads from
Speed = bin.Speed --This is Value B, where it writes to
TopSpeed = 20
-------------------------------------------
function ForwardBack()
	while true do
		if Thrust.Value == 1 then
			if Speed.Value < TopSpeed then
				Speed.Value = (Speed.Value+1)
			end
		elseif Thrust.Value == -1 then
			if Speed.Value > (-TopSpeed/2) then
				Speed.Value = (Speed.Value-1)
			end
		elseif Thrust.Value == 0 then
			if Speed.Value > 0 then
				Speed.Value = (Speed.Value-1)
			elseif Speed.Value < 0 then
				Speed.Value = (Speed.Value+1)
			end
		end
		wait(0.1) --Here's where the issue is, I think?
	end
end
-------------------------------------------
Thrust.Changed:Connect(ForwardBack)

Any idea what might be causing this? I've tried putting waits in each 'Thrust.Value' check but it doesn't change anything.

For reference, here's the script that writes to Thrust.Value.

uis = game:GetService("UserInputService")

bin = script.Parent
Thrust = bin.Thrust
Yaw = bin.Yaw
Alt = bin.Altitude
--------------------------------------------------------------
uis.InputBegan:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W then
		Thrust.Value = (Thrust.Value+1)
	end
	if Input.KeyCode == Enum.KeyCode.S then
		Thrust.Value = (Thrust.Value-1)
	end
	if Input.KeyCode == Enum.KeyCode.A then
		Yaw.Value = (Yaw.Value-1)
	end
	if Input.KeyCode == Enum.KeyCode.D then
		Yaw.Value = (Yaw.Value+1)	
	end
	if Input.KeyCode == Enum.KeyCode.R then
		Alt.Value = (Alt.Value+1)
	end
	if Input.KeyCode == Enum.KeyCode.F then
		Alt.Value = (Alt.Value-1)
	end
end)

uis.InputEnded:Connect(function(Input)
	if Input.KeyCode == Enum.KeyCode.W then
		Thrust.Value = (Thrust.Value-1)
	end
	if Input.KeyCode == Enum.KeyCode.S then
		Thrust.Value = (Thrust.Value+1)
	end
	if Input.KeyCode == Enum.KeyCode.A then
		Yaw.Value = (Yaw.Value+1)
	end
	if Input.KeyCode == Enum.KeyCode.D then
		Yaw.Value = (Yaw.Value-1)	
	end
	if Input.KeyCode == Enum.KeyCode.R then
		Alt.Value = (Alt.Value-1)
	end
	if Input.KeyCode == Enum.KeyCode.F then
		Alt.Value = (Alt.Value+1)
	end
end)
--------------------------------------------------------------

1 answer

✓ Accepted answer

Every time the value of Thrust is changed, the while true do loop is started. When you change its value five times, five loops will be running simultaneously. As far as I can tell, you don't actually need to connect to Thrust.Changed.

bin = script.Parent
Thrust = bin.Thrust --This is Value A, where it reads from
Speed = bin.Speed --This is Value B, where it writes to
TopSpeed = 20
    while true do
        if Thrust.Value == 1 then
            if Speed.Value < TopSpeed then
                Speed.Value = (Speed.Value+1)
            end
        elseif Thrust.Value == -1 then
            if Speed.Value > (-TopSpeed/2) then
                Speed.Value = (Speed.Value-1)
            end
        elseif Thrust.Value == 0 then
            if Speed.Value > 0 then
                Speed.Value = (Speed.Value-1)
            elseif Speed.Value < 0 then
                Speed.Value = (Speed.Value+1)
            end
        end
        wait(0.1)
    end

If you need to wait for Thrust to change before you start the loop, simply add

Thrust.Changed:wait()

before

while true do

1 comment

0

Thanks for the help! But I do have a question. In general terms, does a 'while true do' loop act in the same way as 'var.Event:connect'?

This discussion is preserved as it was. New answers, comments, and votes are closed.