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

while loop not working. Is there a way to fix it or an alternative?

local plr = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local tapped = false
local downed = false
local debounce = false
local sprinting = false
local info1 = TweenInfo.new(0.2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, false, 0)
local anim = script:WaitForChild("Sprint")
local sprintAnim = plr.Character:WaitForChild("Humanoid"):LoadAnimation(anim)

UIS.InputBegan:Connect(function(input, check)
	
	if check == false then
		
		if input.KeyCode == Enum.KeyCode.W then
			
			if tapped == false then
				
				if debounce == false then
					
					tapped = true
					wait(0.25)
					tapped = false
					
				end
				
			else
				
				if debounce == false then
					
					if plr.Stamina.Value >= 3 then
					
						debounce = true
						game.ReplicatedStorage.Events.AdvancedMovement:FireServer("Dash")
						local Anim = script["Forward-Dash"]
						local DashAnim = plr.Character.Humanoid:LoadAnimation(Anim)
						plr.Character.HumanoidRootPart.Velocity = plr.Character.HumanoidRootPart.CFrame.LookVector * 250
						DashAnim:Play()
						wait(0.3)
						debounce = false
						
					end
					
				end
				
			end
			
		elseif input.KeyCode == Enum.KeyCode.LeftControl then 		
			
			if sprinting == false then
				
				if plr.Character.Humanoid.MoveDirection.Magnitude > 0 then
				
					if plr.Stamina.Value >= 5 then
					
						sprinting = true
						plr.Character.Humanoid.WalkSpeed = 25
						sprintAnim:Play()
						TS:Create(workspace.Camera, info1, {FieldOfView = 80}):Play()
						game.ReplicatedStorage.Events.AdvancedMovement:FireServer("SprintStart")
						print(sprinting)
					
					end
					
				end
				
			else
				
				sprinting = false
				TS:Create(workspace.Camera, info1, {FieldOfView = 70}):Play()
				plr.Character.Humanoid.WalkSpeed = 16
				sprintAnim:Stop()
				print(sprinting)
				
			end
	
			print(sprinting)
			
		end
		
	end
	
end)


while sprinting == true do
	
	print(plr.Character.Humanoid.MoveDirection.Magnitude)
	
	if plr.Character.Humanoid.MoveDirection.Magnitude > 0 then
				
		if plr.Stamina.Value >= 1 then
			
			game.ReplicatedStorage.Events.AdvancedMovement:FireServer("SprintOn")
			
		else
			
			sprinting = false
			TS:Create(workspace.Camera, info1, {FieldOfView = 70}):Play()
			plr.Character.Humanoid.WalkSpeed = 16
			sprintAnim:Stop()
			
		end
		
	else
		
		sprinting = false
		TS:Create(workspace.Camera, info1, {FieldOfView = 70}):Play()
		plr.Character.Humanoid.WalkSpeed = 16
		sprintAnim:Stop()
		
	end
	
	wait()
	
end

I've been puzzled as to why the while sprinting == true do loop isn't working. It's not printing anything, and I'm certain that the "sprinting" variable is true due to other methods I've tried. Can I not do this and if so, is there an alternative?

1 answer

✓ Accepted answer

Here is what the script is doing:

  1. Create an RBXScriptConnection that relates to a function fired via the related RBXScriptSignal. (This is UIS.InputBegan.)

  2. If sprinting is true, ignoring the function in the connection, then run a while loop once and only once.

An easy way to fix this is to specifically tell the script when sprinting gets set to true, which is within the function that sets it to true. You could easily just create an if condition to check to see when it becomes true, and when it is true, run the while loop. (Mind you, this has to be inside of the function. It can't be outside of the function.)

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