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

How can I detect if a BoolValue has been changed and run code based on if it's true or false?

Hello, I am trying to set a custom animation when the player is running vs. walking in my game, as you can see at the end of the script. It seems to not be working and I'm getting no errors at all.

I've tried going on discord and the Roblox devforum and looking for solutions, but the ones I've seen haven't worked. Any help would be appreciated, thanks.

I have also tested each animation on it's own and they seem to work perfectly fine. But in this script, only the walking animation is working and when I start sprinting, the running animation isn't playing, but I can see the WalkSpeed has changed.

This script is not on the server, it is on a LocalScript.

local SprintValue = script.Sprint

local walkSpeed = 14
local runSpeed = 20

local SprintKey = "LeftShift"

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode[SprintKey] then
        if check_movement() then
            SprintValue.Value = true
            humanoid.WalkSpeed = runSpeed
        else
            
        end
    end
end)

UIS.InputEnded:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode[SprintKey] then
        if SprintValue.Value == true then
            SprintValue.Value = false
            humanoid.WalkSpeed = walkSpeed
        end
    end
end)

if humanoid.WalkSpeed == runSpeed and SprintValue.Value == false then
    humanoid.WalkSpeed = walkSpeed
end

if SprintValue.Value == true then
    character.Animate.run.RunAnim.AnimationId = "rbxassetid://10149274629" -- run
else
    character.Animate.run.RunAnim.AnimationId = "rbxassetid://10148865176" -- walk
end

1 answer

You use .Changed or :GetPropertyChangedSignal(). I recommend using :GetPropertyChangedSignal() because it is much better and only checks for a specific property, unlike .Changed because it checks all properties, which can cause errors or results that weren't supposed to happen.

Changed:

SprintValue.Changed:Connect(function(propertyName)
	if propertyName:lower() == "value" then
		if SprintValue.Value == true then
			character.Animate.run.RunAnim.AnimationId = "rbxassetid://10149274629
		else
			character.Animate.run.RunAnim.AnimationId = "rbxassetid://10148865176"
		end
	end
end)

GetPropertyChangedSignal:

SprintValue:GetPropertyChangedSignal("Value"):Connect(function()
	if SprintValue.Value == true then
		character.Animate.run.RunAnim.AnimationId = "rbxassetid://10149274629
	else
		character.Animate.run.RunAnim.AnimationId = "rbxassetid://10148865176"
	end
end)

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