So my animations for my gun work fine when I first join the game. however, if I reset my character, my animation declaration is for some reason nil, thus halting the entire gun script so that it doesn’t work anymore.
I get an error in the output saying: LoadAnimation requires the Humanoid Object
.
I’ve tried their solutions, but they do not work.
here is the client code:
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local mouse = game.Players.LocalPlayer:GetMouse()
local onCursorChange = script.Parent:WaitForChild("OnCursorChange")
local tool = script.Parent
local ammo = 10
while char.Parent == nil do
char.AncestryChanged:wait()
end
local idleAnim = char:WaitForChild("Humanoid"):LoadAnimation(script.IdleAnim)
local shootAnim = char:WaitForChild("Humanoid"):LoadAnimation(script.ShootAnim)
print("Got past animation declaration")
tool.Equipped:Connect(function()
idleAnim:Play()
end)
tool.Unequipped:Connect(function()
idleAnim:Stop()
end)
tool.Activated:Connect(function()
shootAnim:Play()
ammo = ammo - 1
script.Parent.Fire:FireServer(mouse.Hit.p)
script.gunshot:Play()
wait(1)
print(ammo)
if ammo <= 0 then
reload()
end
end)
function reload()
script.Reload:Play()
script.Disabled = true
wait(4)
ammo = 10
script.Disabled = false
end
onCursorChange.OnClientEvent:Connect(function(mouseIcon)
mouse.Icon = mouseIcon
end)
Let me know if you need anything else or if this is the wrong category.
Is this in the StarterPlayer or the StarterCharacter? Because if it’s in the StarterPlayer, then I believe when the player resets, the variable for the character does not work anymore. (I’m not an experienced scripter, so this might be wrong).
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local mouse = player:GetMouse()
local tool = script.Parent
local onCursorChange = tool:WaitForChild("OnCursorChange")
local ammo = 10
local shooting = false
local reloading = false
local function Reload()
if not shooting and not reloading then
reloading = true
script.Reload:Play()
wait(4)
ammo = 10
reloading = false
end
end
local animator = humanoid:FindFirstChildOfClass("Animator")
if not animator then
animator = Instance.new("Animator")
animator.Parent = humanoid
end
local idleAnimation = animator:LoadAnimation(script.IdleAnim)
local shootAnimation = animator:LoadAnimation(script.ShootAnim)
tool.Equipped:Connect(function()
idleAnimation:Play()
end)
tool.Unequipped:Connect(function()
idleAnimation:Stop()
end)
tool.Activated:Connect(function()
if not reloading then
shooting = true
ammo -=1
script.Parent.Fire:FireServer(mouse.Hit.p)
script.gunshot:Play()
wait(1)
shooting = false
if ammo <= 0 then
Reload()
end
end
end)
onCursorChange.OnClientEvent:Connect(function(mouseIcon)
mouse.Icon = mouseIcon
end)