Hello! I was trying to test out an animation when I got this error:
Workspace.WolfieGamerYT.ProximityPrompt.Script:4: attempt to index nil with 'Character'
Here is my code:
local Players = game:GetService("Players")
local Player = Players:FindFirstChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "https://web.roblox.com/library/14639655080/Punch"
local AnimationTrack = Humanoid:LoadAnimation(Animation)
script.Parent.Triggered:Connect(function()
script.Parent:WaitForChild("Sound"):Play()
Animation:Play()
end)
If you think you know went when wrong, please let me know. Thank you and have a wonderful day!
trying to get a specific player in a server script via player name isnt a good approach, since the script runs before the player object gets added, and sometimes the player isnt in the server, but you said you are testing.
For your testing, Use WaitForChild() instead.
By the way, you should make the animationtrack declared in the function.
like:
local Players = game:GetService("Players")
local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "https://web.roblox.com/library/14639655080/Punch"
script.Parent.Triggered:Connect(function()
local AnimationTrack = Humanoid:LoadAnimation(Animation)
script.Parent:WaitForChild("Sound"):Play()
Animation:Play()
end)
I just realized, You are trying to play the animation object, you have to play the animation track.
Instead of “Animation:Play()” Do “AnimationTrack:Play()”
local Players = game:GetService("Players")
local Player = Players:FindFirstChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://14639655080"
local AnimationTrack = Humanoid:LoadAnimation(Animation)
script.Parent.Triggered:Connect(function()
script.Parent:WaitForChild("Sound"):Play()
AnimationTrack:Play()
end)
Hm, Im not sure, ive never seen that error before.
Make sure that you own the animation ID you are trying to play, or make sure that the ID is in fact an animation.
Oh thats right, I wasnt looking at the animation ID itself, maybe that will work then?
I believe this will be the full script
local Players = game:GetService("Players")
local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://14639655080"
script.Parent.Triggered:Connect(function()
local AnimationTrack = Humanoid:LoadAnimation(Animation)
script.Parent:WaitForChild("Sound"):Play()
Animation:Play()
end)
I see, Maybe the character hasnt been added, so try this:
local Players = game:GetService("Players")
local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://14639655080"
script.Parent.Triggered:Connect(function()
local AnimationTrack = Humanoid:LoadAnimation(Animation)
script.Parent:WaitForChild("Sound"):Play()
Animation:Play()
end)
maybe replacing the FindFirstChild with Waitforchild will fix it
local Players = game:GetService("Players")
local Player = Players:WaitForChild("TabbyCat904")
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://14639655080"
local AnimationTrack = Humanoid:LoadAnimation(Animation)
script.Parent.Triggered:Connect(function()
script.Parent:WaitForChild("Sound"):Play()
AnimationTrack:Play()
end)
I just have one question: I also want a GUI label to appear whenever the prompt is triggered. Would that be better to do locally or should I just try it in a regular script?
You could do the entire thing locally, since animations are replicated to the server.
But if it has to be server-sided, Then you can place the GUI in the Player’s “PlayerGui”