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

I've almsot perfected it PLEASE help with this ONE PROBLEM?! (SOLVED)

Ok I AM ALMOST DONE WITH THIS ANIMATION SCRIPT!! JUST 1 MORE PROBLEM!!

People love Thumbing down my questions as if I've done something wrong.. and it's annoying... when I ask a question someone thumbs down of course and they dont even bother to answer.

Ok I made this and, when I press and Hold Q my WalkSpeed goes to 30 like I'm asking it to, AND my run animation starts! So, the first part is perfect.. but when I Release Q My Walkspeed Returns to 16 as it should but my animation doesnt stop! How do I stop it?!

wait(2)

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()
local humanoid = player.Character.Humanoid --Humanoid

Mouse.KeyDown:connect(function(key)
if key:lower() == "q" and humanoid then --I added ":lower()" incase they had caps lock on. I also added "and humanoid" just to see if it exists.
    s = humanoid:LoadAnimation(game.StarterPack.AnimScrip2.Animation)
    s:Play()
    humanoid.WalkSpeed = 30 --Change the speed
end
end)


Mouse.KeyUp:connect(function(key)
	if key:lower() == "q" and humanoid then
		s = humanoid:LoadAnimation(game.StarterPack.AnimScrip2.Animation)
		s:Stop()
		humanoid.WalkSpeed = 16
	end
end)

1 comment

0

try making the "s" variable global.

1 answer

✓ Accepted answer

Make your animation variable s global.

repeat
	wait()
until game.Players.LocalPlayer

player = game.Players.LocalPlayer
character = player.Character
if not character then
	character = player.CharacterAdded:wait()
end

Mouse = player:GetMouse()
humanoid = character:WaitForChild("Humanoid")
s = humanoid:LoadAnimation(game.StarterPack.AnimScrip2.Animation)

-- It's best if this variable was global, because then the script would be able to refer to the animation more efficiently.

-- Every time you load an animation to a variable, it inserts a new Animation instance in the Humanoid to be played. Kind of like "holding" an animation in the Humanoid.

-- So if you used your script & when you click "q" & release it, the Animation instance that's loaded in your Humanoid will be created 2 times, since you loaded it twice (through your .KeyDown/.KeyUp functions).

-- But in this script, the animation is loaded only ONCE.

Mouse.KeyDown:connect(function(key)
	if key:lower() == "q" then
		s:Play()
		humanoid.WalkSpeed = 30
	end
end)


Mouse.KeyUp:connect(function(key)
	if key:lower() == "q" then
		s:Stop()
		humanoid.WalkSpeed = 16
	end
end)

4 comments

0

You beat me :(

0

xD Sorry.

0

I do like Bumping up your Reputation tho.

0

Thanks for your help! And thanks to the people that tried!

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