Why is my script not working?

Im trying to make a animation script that has a state string that will be changed whenever the humanoid state changes, but it always prints out what it currently was and Is never changed.

There’s no errors eathier.

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")

local idleAnim = Instance.new("Animation")
idleAnim.AnimationId = "rbxassetid://8425357501"
local idleAnimTrack = humanoid:LoadAnimation(idleAnim)

local runAnim = Instance.new("Animation")
runAnim.AnimationId = "rbxassetid://10109174246"
local runAnimTrack = humanoid:LoadAnimation(runAnim)

local rollAnim = Instance.new("Animation")
runAnim.AnimationId = "rbxassetid://10109304466"
local rollAnimTrack = humanoid:LoadAnimation(runAnim)

local jumpAnim = Instance.new("Animation")
jumpAnim.AnimationId = "rbxassetid://10118425407"
local jumpAnimTrack = humanoid:LoadAnimation(jumpAnim)

local JumpSound = Instance.new("Sound")
JumpSound.SoundId = "rbxassetid://8396408200"
JumpSound.Volume = 2.7
JumpSound.Parent = humanoid.Parent:WaitForChild("Torso")

local humanoidStates = {
	"Running",
	"Jumping",
	"FreeFall",
	"Swimming",
	"Landed",
	"Climbing",
	"Idle"
}

local state = ""

local UIS = game:GetService("UserInputService")

idleAnimTrack:Play()

while true do
	task.wait()
	print(state)
	if state == "Running" then
		runAnimTrack:Play()
		idleAnimTrack:Stop()
		jumpAnimTrack:Stop()
	else
		if state == "Jumping" then
			jumpAnimTrack:Play()
			runAnimTrack:Stop()
			idleAnimTrack:Stop()
		else
			if state == "FreeFall" then
				-- Play Free fall
			else
				if state == "Swimming" then
					-- Play Swimg
				else
					if state == "Landed" then
						idleAnimTrack:Play()
						runAnimTrack:Stop()
						jumpAnimTrack:Stop()
					else
						if state == "Climbing" then
							-- Playing Climbing Animation
						else
							if state == "Idle" then
								idleAnimTrack:Play()
								runAnimTrack:Stop()
								jumpAnimTrack:Stop()
							end
						end
					end
				end
			end
		end
	end
end

UIS.InputBegan:Connect(function(input, gameProcessed)
	if not gameProcessed and input == Enum.KeyCode.E then
		local RollBoost = Instance.new("LinearVelocity")
		RollBoost.MaxForce = 99999999999

		local Attatchment = Instance.new("Attachment")
		RollBoost.Attachment1 = Attatchment
		RollBoost.Attachment0 = Attatchment

		Attatchment.Parent = script.Parent:WaitForChild("HumanoidRootPart")
	end
end)

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		state = tostring(humanoidStates[1])
	else
		if speed == 0 then
			state = tostring(humanoidStates[7])
		end
	end
end)

humanoid.Jumping:Connect(function()
	state = tostring(humanoidStates[2])
end)

humanoid.FreeFalling:Connect(function()
	state = tostring(humanoidStates[3])
end)

humanoid.Swimming:Connect(function()
	state = tostring(humanoidStates[4])
end)

if humanoidStates.Landed then
	state = tostring(humanoidStates[5])
end

humanoid.Climbing:Connect(function()
	state = tostring(humanoidStates[6])
end)
1 Like

Off the bat, I see what appears to be a typo.

local runAnim = Instance.new("Animation")
runAnim.AnimationId = "rbxassetid://10109174246"
local runAnimTrack = humanoid:LoadAnimation(runAnim)

local rollAnim = Instance.new("Animation")
runAnim.AnimationId = "rbxassetid://10109304466"    <----
local rollAnimTrack = humanoid:LoadAnimation(runAnim)    <----

Have you tried looking at this: Humanoid | Roblox Creator Documentation? There’s a state changed event that gives both the old and new states.

2 Likes
while true do
	task.wait()
	print(state)
	if state == "Running" then
		runAnimTrack:Play()
		idleAnimTrack:Stop()
		jumpAnimTrack:Stop()
	else
		if state == "Jumping" then
			jumpAnimTrack:Play()
			runAnimTrack:Stop()
			idleAnimTrack:Stop()
		else
			if state == "FreeFall" then
				-- Play Free fall
			else
				if state == "Swimming" then
					-- Play Swimg
				else
					if state == "Landed" then
						idleAnimTrack:Play()
						runAnimTrack:Stop()
						jumpAnimTrack:Stop()
					else
						if state == "Climbing" then
							-- Playing Climbing Animation
						else
							if state == "Idle" then
								idleAnimTrack:Play()
								runAnimTrack:Stop()
								jumpAnimTrack:Stop()
							end
						end
					end
				end
			end
		end
	end
end

This a bad code habit, use ‘elseif’ not ‘else if’, the former continues the current conditional chain, the latter creates a new conditional chain.

1 Like

I don’t understand were the typo is specifically?

I changed it to this, but state is still not changing and is only printing out a white space.

while true do
    task.wait()
    print(state)
    if state == "Running" then
        runAnimTrack:Play()
        idleAnimTrack:Stop()
        jumpAnimTrack:Stop()
    elseif state == "Jumping" then
        jumpAnimTrack:Play()
        runAnimTrack:Stop()
        idleAnimTrack:Stop()
    elseif state == "FreeFall" then
        -- Play Free fall
    elseif state == "Swimming" then
        -- Play Swimg
    elseif state == "Landed" then
        idleAnimTrack:Play()
        runAnimTrack:Stop()
        jumpAnimTrack:Stop()
    elseif state == "Climbing" then
        -- Playing Climbing Animation
    elseif state == "Idle" then
        idleAnimTrack:Play()
        runAnimTrack:Stop()
        jumpAnimTrack:Stop()
    end
end

Well, you define rollAnim, but on the next line, you do the ID for runAnim, and then you load the track for runAnim and place it in rollAnim. It wouldn’t matter too much since the track does get loaded with the correct animation, but it’s something that I saw.

1 Like

I’m curious as to why you aren’t using Humanoid.StateChanged event. Anytime the humanoid state changes, the engine will signal your script.