How to wait for a bool value to change

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to stop sprinting as soon as the “Sprintable” boolvalue is false

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have gone through many posts on the developer hub.
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local UIS = game:GetService('UserInputService')
local Player = game.Players.LocalPlayer
local Character = Player.Character
local sprintable = Character:WaitForChild('Sprintable')
local sprinting = Character.Sprinting
players = game:GetService('Players')
player = players.LocalPlayer
char = game.Workspace[player.Name]
frame = player.PlayerGui.Stamina.bar

UIS.InputBegan:connect(function(input)
 if input.KeyCode == Enum.KeyCode.LeftShift and sprintable.Value == true then
		if frame.Size.X.Offset >= 0 then	
		Character.Humanoid.WalkSpeed = 25
		local Anim = Instance.new('Animation')
		Anim.AnimationId = 'rbxassetid://8308902269'
		Character.Humanoid.JumpPower = 0
		PlayAnim = Character.Humanoid:LoadAnimation(Anim)
		sprinting.Value = true
		PlayAnim:Play()
			wait()
			Character.Humanoid.WalkSpeed = 16
			Character.Humanoid.JumpPower = 50
			PlayAnim:Stop()
			sprinting.Value = false
		else
			return
		end
	end
end)


UIS.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift and sprintable.Value == true then
		Character.Humanoid.WalkSpeed = 16
		Character.Humanoid.JumpPower = 50
		PlayAnim:Stop()
		sprinting.Value = false
 end
end)

Im new to scripting so dont mind the bad script
Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like
--part of InputBegan code
PlayAnim:Play()
--keeps waiting until the condition(not sprintable.Value) is met
repeat task.wait() until not sprintable.Value 
Character.Humanoid.WalkSpeed = 16
Character.Humanoid.JumpPower = 50
PlayAnim:Stop()
--no reason to set the value to false, it must be false to get here

the value is for stamina and animations so you cant reset the walk and jump speed by pressing shift

sorry that i didnt mention that

What should the intended behavior be?

there is a localscript that sets the sprintable bool to false when the stamina of the player reaches zero, the sprint script should stop sprinting when the bool is set to false

Try running my code again, I think I accidently used the sprinting value instead of sprintable.

Thank you, this worked. It also turns out that the sprintable disabling script didnt have the sprintable disabler at all.

1 Like