Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

0

That's strange. Someone else had a problem with it, too.

1 answer

✓ Accepted answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.