local player = game:GetService("Players").LocalPlayer
local character = player.Character
local animation = script.Parent:FindFirstChild("Animation")
local PlayAnim = character.Humanoid:LoadAnimation(animation)
local canPlay = true
local debounce = 1.5
script.Parent.Activated:Connect(function()
if canPlay then
canPlay = false
PlayAnim:Play()
wait(debounce)
canPlay = true
end
end)
code is for whenever you use a gear, it plays that animation.
the error I get is attempt to index nil with humanoid. Anyone know a fix?
You should always use WaitForChild() with a repeat/timeout value.
local character = player.Character
if not character or not character.Parent then
character = player.CharacterAdded:wait()
end
local humanoid = character:WaitForChild("Humanoid",3);
You can do it instantaneously do by doing :FindFirstChildOfClass and skip it on the Humanoid.
local Players = game.Players
local Animation = script.Parent:FindFirstChild("Animation")
local Humanoid = Players:FindFirstChildOfClass("Humanoid")
local Play_The_Animation = character.Humanoid:LoadAnimation(animation)
This is how the 1st 4 lines should be.
The rest of the script should be:
local Players = game.Players
local Animation = script.Parent:FindFirstChild("Animation")
local Humanoid = Players:FindFirstChildOfClass("Humanoid")
local Play_The_Animation = character.Humanoid:LoadAnimation(Animation)
local Can_Play = true
local Debounce = 1.5
script.Parent.Activated:Connect(function()
if Can_Play then
Can_Play = false
Play_The_Animation:Play()
task.wait(Debounce)
Can_Play = true
end
end)
It could work, if it doesn’t work, you probably copied the 1st script and not the full script.
Well, you know in my Button Simulation Game, you know that the hit.Parent is a Instance, by adding :FindFirstChildOfClass it directly to the Character and you can see anything, anything that you can, but some of these you can’t, like the Humanoid, in this version of the :FindFirstChildOfClass, you can see anything, and here’s the reason why it’s not working when doing on the, you can’t do it manually, by doing game.Players.LocalPlayer.Character.Humanoid (Well technically yes on a LocalScript because it has a permission to allow to see the LocalPlayer which permits to Kick The Player That Wanted To Quit The Game If They Are Bored, or by Going To The Character, but in fact you need to manually type it if it’s a serverscript.).
Like This Script:
local config = script.Parent.Config
script.Parent.Part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChildOfClass("Humanoid") then
if game.Players:GetPlayerFromCharacter(hit.Parent) then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr:FindFirstChild("leaderstats") and plr.leaderstats:FindFirstChild("Rebirth") then
if plr.leaderstats.Cash.Value >= config.Cost.Value then
plr.leaderstats.Multiplier.Value = plr.leaderstats.Multiplier.Value + config.Amount.Value
plr.leaderstats.Cash.Value = plr.leaderstats.Cash.Value - config.Cost.Value
plr.leaderstats.Multiplier.Value = plr.leaderstats.Rebirth.Value + plr.leaderstats.Multiplier.Value-1
print("You got "..plr.leaderstats.Rebirth.Value.." Multiplier and lost "..config.Cost.Value.." Cash. So now you have "..plr.leaderstats.Cash.Value.." Cash and "..plr.leaderstats.Multiplier.Value.." Multiplier.")
end
end
end
end
end)
script.Parent.Part.GUI.TextLabel.Text = "$"..config.Cost.Value.." = x"..config.Amount.Value
Edit: Edited. Also, read the parentheses that I posted.
Bro, but what does it has to do with op’s problem? Take your pills, btw 4 nested ifs . You absolutely can access the humanoid of a character on a server.
The issue with OP’s post is that the humanoid hasn’t loaded in yet, and therefore returns nil when referenced.
Your method, whilst useful when handling objects that have the potential to be named unconventionally, won’t work, as :FindFirstChildOfClass() returns nil if the object isn’t found. Same problem.
Instead, when using your method, you should be trying something like:
local character : Model = plyr.Character or plyr.CharacterAdded:Wait()
local hum : Humanoid = character:FindFirsChildOfClass("Humanoid")
while not hum do
hum = character:FindFirsChildOfClass("Humanoid")
task.wait()
end
The thread will not execute past this point whilst there isn’t a Humanoid-class object present.
local player = game:GetService("Players").LocalPlayer
local character = script.Parent.Parent -- script.Parent = Tool, script.Parent.Parent = The Player
local animation = script.Parent:FindFirstChild("Animation")
local PlayAnim = character:WaitForChild("Humanoid"):LoadAnimation(animation)
local canPlay = true
local debounce = 1.5
script.Parent.Activated:Connect(function()
if not canPlay then
canPlay = false
PlayAnim:Play()
wait(debounce)
canPlay = true
end
end)
local character = player.Character or player.CharacterAdded:Wait()
Basically the second one checks if the character is loaded first and if it isn’t it waits for the character to load and then assigns the character to the variable.