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

Script to sprint with animation(using the animate script's animations)?

if not game.Players.LocalPlayer.Character then
	wait()
end

local UIS = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local chr = plr.Character

while true do
walkAnim = chr.Animate.walk.WalkAnim
runAnim = chr.Animate.run.RunAnim

UIS.InputBegan:connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		chr.Humanoid.WalkSpeed = 24
		chr.Humanoid:LoadAnimation(runAnim)
	end
end)

UIS.InputEnded:connect(function(key)
	if key.KeyCode == Enum.KeyCode.LeftShift then
		chr.Humanoid.WalkSpeed = 12
		chr.Humanoid:LoadAnimation(walkAnim)
	end
end)

wait()	
end

I feel like this script should work, I have the animation Ids right, so I do not know what the problem is. It is just running the Walk animation. It still changes WalkSpeed though. I set the Run animation to an id, instead of it being run.xml or whatever it usually is. Maybe the problem is in the animation script and not this, I dunno...

1 answer

The script needs to be a regular script, and because it needs to be a ordinary script, the script has to be fitted for a ordinary script. Below will be your script, that I have fixed.


-- //Put this in StarterCharacterScripts

local UIS = game:GetService("UserInputService")
local plr = game.Players:GetPlayerFromCharacter(script.Parent)
local chr = plr.Character

while true do
walkAnim = chr.Animate.walk.WalkAnim
runAnim = chr.Animate.run.RunAnim

UIS.InputBegan:connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        chr.Humanoid.WalkSpeed = 24
        chr.Humanoid:LoadAnimation(runAnim)
    end
end)

UIS.InputEnded:connect(function(key)
    if key.KeyCode == Enum.KeyCode.LeftShift then
        chr.Humanoid.WalkSpeed = 12
        chr.Humanoid:LoadAnimation(walkAnim)
    end
end)

wait()  
end

2 comments

0

Hmm, still not running the right animation...

0

All fixed, chr.Humanoid:LoadAnimation(runAnim) should of been a local variable, then I just ran the animation!

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