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

[SOLVED] While loop should stop, but it's not stopping?

This is kinda hard for me to explain, but basically, I have this local script where when I press or let go of left shift, it fires my remote event. When I press left shift it fires my remote event with an argument of true. When I let go of left shift it fires my remote event with argument of false.

On my server script where I have the OnServerEvent function, I have a while loop which is supposed to run only when the parameter is true, (so when I press left shift), except it keeps on running when I let go of left shift. Does anyone know why? Any help would be appreciated.

Local Script ----

local UIS = game:GetService("UserInputService")
local sprintEvent = script.Parent:WaitForChild("Sprint")

UIS.InputBegan:Connect(function(input,gpe)
	if gpe then return end
	
	if input.KeyCode == Enum.KeyCode.LeftShift then
		sprintEvent:FireServer(true)
	end	
end)

UIS.InputEnded:Connect(function(input,gpe)
	if gpe then return end

	if input.KeyCode == Enum.KeyCode.LeftShift then
		sprintEvent:FireServer(false)
	end	
end)

Server Script ----

local sprintEvent = script.Parent:WaitForChild("Sprint")

sprintEvent.OnServerEvent:Connect(function(player,switch)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	
	while(switch) do
		wait(1)
		print("Sprinting")
	end
	
	if switch == false then
		print("Not Sprinting")
	end
end)

The while loop keeps on running when I let go of left shift, but the switch parameter should be false

2 answers

✓ Accepted answer

I believe this is because the variable "switch" when you first press the shift key is a different variable than the "switch" variable when you release it, two different instances, even though they have the same name. You will have to find a different way to store this value. Here is a possible solution, where you create a bool value in the player:

local sprintEvent = script.Parent:WaitForChild("Sprint")

game.Players.PlayerAdded:Connect(function(plyr)
	local running = Instance.new("BoolValue")
	running.Name = "IsRunning"
	running.Value = false
	running.Parent = plyr
end)

function printIsRunning(plyr)
	while plyr.IsRunning.Value == true do
		wait(1)
		print("Player is running!")
	end
end

sprintEvent.OnServerEvent:Connect(function(player,switch)
   	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:WaitForChild("Humanoid")
	
	if player:FindFirstChild("IsRunning") then
		player.IsRunning.Value = switch

		if switch then
			printIsRunning(player)
		end
	end
end)

1 comment

0

Hi! I did something similar to what u said, and I got it to work. Thank you!

For the ServerScript, try doing this

local sprintEvent = script.Parent:WaitForChild("Sprint")

sprintEvent.OnServerEvent:Connect(function(player,switch)
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoid = character:WaitForChild("Humanoid")

    while(switch) do
        wait(1)
        print("Sprinting")
	if switch == false then
	    break
            print("Not Sprinting")
        end
    end
end)

4 comments

0

I tried what you said but the while loop still keeps on running. It still prints 'Sprinting'. I'm not sure why it's not working

0

either do this in line 7: while switch == true then, or remove that extra end at line 13

0

Lol that didn't work either, but I figured it out. Thank you!

0

You're most certainly welcome!

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