Why is my script not working?

Im trying to make a animations script and also a script that will play sounds for my custom character, Oofy.

Script:

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

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

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

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

while true do
	task.wait()
	humanoid.Running:Connect(function(speed)
		if speed == 0 then
			idleAnimTrack:Play()
		else
			if speed > 0 then
				runAnimTrack:Play()
				else
				runAnimTrack:Stop()		
			end	
		end
	end)
end

while true do
	task.wait()
	if humanoid.Jump == true then
		JumpSound:Play()	
	end
end

Theirs are no errors or anything in the console, and I don’t understand what the issue is!

Remove the first while true do loop(without removing the inside). It spams the humanoid.Running event and prevents the other loop from running. Also the other loop isn’t needed, you can switch to an event based solution:

humanoid.Jumping:Connect(function()
	JumpSound:Play()
end)
1 Like

Ok so, I can hear the jump sound animation now! but, the animations don’t work!

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

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

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

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

humanoid.Running:Connect(function(speed)
	if speed == 0 then
		idleAnimTrack:Play()
	else
		if speed > 0 then
			runAnimTrack:Play()
		else
			runAnimTrack:Stop()
		end
	end
end)

humanoid.Jumping:Connect(function()
	JumpSound:Play()
end)

Your if statement is a bit weird, consider changing it to:

if speed > 0 then
	idleAnimTrack:Stop()
	runAnimTrack:Play()
else
	runAnimTrack:Stop()
	idleAnimTrack:Play()
end
1 Like

Still doesn’t seem to work, do you think I should just do connect:function() and then if Humanoid.WalkSpeed = x then?

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

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

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

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

humanoid.Running:Connect(function(speed)
	if speed == 0 then
		idleAnimTrack:Play()
	elseif speed > 0 then
		runAnimTrack:Play()
	else
		runAnimTrack:Stop()		
	end
end)

humanoid.Jumping:Connect(function(joe)
	if joe then JumpSound:Play() end
end)

Can you explain to me the changes you made? Im not following.

I also want to learn from this so it would be harder to learn if I just copied and pasted the script.

  • Made JumpSound.Volume set before the parent is set cos that more efficent (always set the parent last)

  • Replaced 2 unnecessary infinite loops with connections instead

Replaced

else
    if speed > 0 then

with

elseif speed > 0 then

Humanoid.WalkSpeed isn’t related to the humanoid current speed.

what is the difference of using elseif?

Just shorter and easier to read.

1 Like