Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Is there a way to fix this error about an object not being a descendant of the game?

Asked by 6 years ago

I'm having this error where Humanoid is not the descendant of the game when trying to load an animation.

Here is the script; [NOTE]: It's in a LocalScript and not Filtering Enabled.

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:wait()
local mouse = player:GetMouse()
local hum = char:WaitForChild("Humanoid")

local data = player:WaitForChild("Data")
local strength = data:WaitForChild("Strength")
local levelxp = data:WaitForChild("LevelXP")
local strengthxp = data:WaitForChild("StrengthXP")

local can = true
local animation = script:WaitForChild("PunchAnimation")
local anime = hum:LoadAnimation(animation)

mouse.KeyDown:Connect(function(pressed)
    local key = pressed:lower()
    if key == "f" and can and anime and char:FindFirstChildOfClass("Tool") == nil then
        for i,child in pairs(char:GetChildren()) do
            if child:IsA("Tool") then return end
        end
        can = false
        levelxp.Value = levelxp.Value + 1
        strengthxp.Value = strengthxp.Value + 2
        anime:Play()
        local s = script:WaitForChild("DamageScript"):Clone()
        s.Parent = char:WaitForChild("Right Arm")
        s.Disabled = false
        local creator = Instance.new("ObjectValue")
        creator.Name = "Creator"
        creator.Value = char
        creator.Parent = s
        local damage = Instance.new("IntValue")
        damage.Name = "Damage"
        damage.Value = (((strength.Value^2)/100)*20)
        damage.Parent = s
        anime:Play()
        anime.Stopped:wait()
        anime:Stop()
        s:Destroy()
        can = true
    end
end)
0
That's strange. Someone else had a problem with it, too. hiimgoodpack 2009 — 6y

1 answer

Log in to vote
0
Answered by
Mineloxer 187
6 years ago

I had this problem before, the problem is that when your character dies, and you have a variable referencing it in a script. The variable doesn't reference the new character, so it turns into nil.(Because it was referencing the old character model which has been destroyed)

A simple fix for this is getting the character, the humanoid and loading the animation all once the function is called. This will always reference the new character.

Ad

Answer this question