This script should be playing an animation based on the value within the script. But instead I get this error.
00:34:49.183 - Players.Player1.PlayerGui.PlayerInputManager:50: attempt to index field 'Character' (a nil value)
The script is inserted into the players gui when a guibutton is clicked. Thanks for any help.
local player = game:GetService('Players').LocalPlayer
local already_ran = false
local grounded = false
local idleAnimation = Instance.new("Animation")
idleAnimation.AnimationId = "rbxassetid://356344446"
local Left = Instance.new("Animation")
Left.AnimationId = "rbxassetid://356344643"
local Right = Instance.new("Animation")
Right.AnimationId = "rbxassetid://326922088"
local Forward = Instance.new("Animation")
Forward.AnimationId = "rbxassetid://312976450"
local Backward = Instance.new("Animation")
Backward.AnimationId = "rbxassetid://312976450"
local Jump = Instance.new("Animation")
Jump.AnimationId = "rbxassetid://326922088"
function playAnimation(parent)
local AnimTrack = player.Character.Humanoid:LoadAnimation(parent)
AnimTrack:Stop()
wait(.01)
AnimTrack:Play()
end
function onGround(speed)
if grounded == false then
grounded = true
end
if speed > 1 then
if already_ran == false then
already_ran = true
playAnimation(Left)
end
elseif speed > 0 and already_ran == true then
already_ran = false
playAnimation(idleAnimation)
end
end
player.Character.Humanoid.Running:connect(onGround)
function onKeyPress(inputObject, gameProcessedEvent)
if inputObject.KeyCode == Enum.KeyCode.A then
print("Left!!")
script.Asset.Value = ("Left")
elseif inputObject.KeyCode == Enum.KeyCode.D then
script.Asset.Value = ("Right")
elseif inputObject.KeyCode == Enum.KeyCode.W then
script.Asset.Value = ("Forward")
elseif inputObject.KeyCode == Enum.KeyCode.S then
script.Asset.Value = ("Backward")
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)

1 comment
To use game.Players.LocalPlayer you would have to use a local script.