How do I properly Reference the character or Humanoid on tool?

I’m having a huge problem that I can’t get my head around for days! or missing some important knowledge or niche that I have to look for.

I somehow or can’t properly reference the humanoid on local script inside the tool located on the starter pack. I get this error.

These is the variables I set up

local Tool = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid", 100)
local animator = humanoid:WaitForChild("Animator", 100)
local toggle = animator:LoadAnimation(script:WaitForChild("Toggle", 100))

The last variable toggle is breaking since somehow humanoid is not there or I don’t know. But I specifically Waited for it. and without the second argument on WaitForChild(object, timeout) it will error too because of timeout.

I had to do this messy style to fix it temporarily but it’s not consistent and still gets the same error from time to time when resetting/dying.

task.wait(0.1)
game["Run Service"].Heartbeat:Wait()
--Variables init down here v

I tried also putting the local script inside StarterPlayerScripts and works wonders but I need the local script on the tool. Please help!

1 Like

I think this may help

Thank you I will check it out.

2 Likes

I applied it hopefully this works. It’s just weird that only the unlucky players are to experience the error and not me on Live Servers. I will checkmark the solution for now. Because I read that the solution does not fix the problem also sometimes.

Removed the solution… It Does not work whatsoever… I’m bumed.

If the script is directly under the tool, you can use this cheaty code to get the player:

local PLR = game:GetService("Players")
local player = script:FindFirstAncestorWhichIsA("Player") or PLR:GetPlayerFromCharacter(script.Parent.Parent)

I’d love to do this and it’s the back of my mind also. But this is probably not consistent either although it will work for me, some players do not. which is so weird and not consistent.

hi you can show the list of objects?

how
image

that helps

1 Like

Here’s the structure of that helps…
image

Tool is placed under StarterPack

you can use Priority of animation
you can put on Action
if you Priority of your animation is idle or something that no is Action the walk,jump,etc… have more priority and shows and not show your animation

test it

This is normally why I only ever assign the Humanoid when the tool is equipped. I don’t want to rely on any weird tricks (i.e. AncestryChanged on the Animator) but I want to keep an event-driven manner so the only other option in mind is to set character variables when the tool’s actually equipped.

You can then run a setup function if it’s never been ran before which will define your upvariables (character, humanoid and the animator) and then load the animation. By that point the Animator should be a descendant of the DataModel and be able to load animations.

The main problem is that scripts in the backpack can execute and the character is not guaranteed to be spawned in or fully assembled when that happens. Characters work on an arcane event ordering that introduces race conditions like this (Animator exists but is not a descendant of the DataModel).

1 Like

I also had that problem, and I just decided to load the animation when I’m sure that the Humanoid has properly loaded, like when the tool is equipped for example.

But now that i think about it, why not just use pcall() ?

Something like:


local toggle
while true do
	local yes,no = pcall(function()
		toggle = animator:LoadAnimation(script.Toggle)
	end)
	if yes then
		break
	end
	wait()
end

I think it would be a waste of resources to do a while loop?
I Fixed it by initializing it when equipped but it has sort of delay when first equipped. In my opinion, this needs to fix engine-wise.

1 Like

Yeah, the delay when it’s first equipped is the only problem to the “load the animations when the tool is equipped” solution. That’s why i suggested the while loop solution.