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

Why is my script not working?

This is an in game script that runs an animation when the f key is down and stops when it is up.

player = game:GetService("Players").LocalPlayer
char = player.Character
mouse = player:GetMouse()
local hum = char:FindFirstChild("Humanoid")
local anim = Instance.new("Animation",char)
anim.AnimationId = "http://www.roblox.com/asset?id=149359445"
anim.Name = "Animation"

mouse.KeyDown:connect(function(key)
if key:lower() == "f" then
animation = hum:LoadAnimation(char.anim)
animation:Play()
end
end)

mouse.KeyUp:connect(function(key)
if key:lower() == "f" then
animation:Stop()
end
end)

1 answer

✓ Accepted answer

Try this. You only called the animation variable in the KeyDown event so it will only work in the KeyDown event.

player = game:GetService("Players").LocalPlayer
char = player.Character
mouse = player:GetMouse()
local hum = char:FindFirstChild("Humanoid")
local anim = Instance.new("Animation",char)
anim.AnimationId = "http://www.roblox.com/asset?id=149359445"
anim.Name = "Animation"
local animation = hum:LoadAnimation(anim)

mouse.KeyDown:connect(function(key)
if key:lower() == "f" then
animation:Play()
end
end)

mouse.KeyUp:connect(function(key)
if key:lower() == "f" then
animation:Stop()
end
end)

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