Hello, I am working on my own gun system using ray casting.
But it seems like whenever you die and try to equip the Gun it does not load the animation and the whole script stops working due to that error.
Error: "LoadAnimation Requires the humanoid object (Idkr_Man.Humanoid) to be a descendant of the game object.
I have tried using WaitForChild and if statements etc but it still gives the same error.
Script:
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Human = Char:WaitForChild("Humanoid")
local Tool = script.Parent
local HoldAnim = Tool:WaitForChild("Hold")
local FireAnim = Tool:WaitForChild("Fire")
local Equipped = false
local Debounce = false
local Mouse = Player:GetMouse()
local HoldTrack
local FireTrack
repeat wait() until Human
local HoldTrack = Human:LoadAnimation(HoldAnim)
local FireTrack = Human:LoadAnimation(FireAnim)
local onEquip = function()
if Player and Player.Character and Player.Character.Humanoid and HoldTrack and FireTrack then
Equipped = true
HoldTrack:Play()
end
end
local onDequip = function()
if Player and Player.Character and Player.Character.Humanoid and HoldTrack and FireTrack then
Equipped = false
HoldTrack:Stop()
end
end
local onFire = function()
if Equipped then
if not Debounce then
if Player and Player.Character and Player.Character.Humanoid and HoldTrack and FireTrack then
Debounce = true
FireTrack:Play()
local ray = Ray.new(Tool:WaitForChild("FirePart").CFrame.Position, (Mouse.Hit.p - Tool:WaitForChild("FirePart").CFrame.Position).unit * 300)
local part,position = workspace:FindPartOnRay(ray, Char, false, true)
Tool:WaitForChild("RemoteEvent"):FireServer(part,position)
wait(.3)
Debounce = false
end
end
end
end
Tool.Activated:Connect(onFire)
Tool.Equipped:Connect(onEquip)
Tool.Unequipped:Connect(onDequip)
Thanks in advance.