Problem with how tool local scripts work

The problem is when the tool gets copied back to the Backpack of the player from the starterpack the character hasn’t been set to the new character yet, Player.Character still references the old character

local Player = game.Players.LocalPlayer

local Character = Player.Character or Player.CharacterAdded:Wait()

local Root = Character:WaitForChild('HumanoidRootPart')

Root.Anchored = true

This simple script has problems, when parented to a tool inside starterpack. When the player resets the tool gets copied however the player object still holds the old reference. So when you spawn it makes your old character’s humanoid rootpart anchored and not yours.

Basically how would I fix this?

2 Likes

Using Ancestry Changed, you can redefine the parent/humanoid whenever the script moves.

1 Like

Unfortunately there is no other easy way to get a character. This should not be happening in the first place, as when you respawn your Local Character becomes the newly created one.

You could reference the Player from parentage of the Backpack, but I don’t know of any other way to get characters, and you shouldn’t have to make any workarounds in the first place.

Normally the old character despawns before the new one appears. Do you have some kind of preservation of old character system?

1 Like

I don’t think the script is actually moving anywhere since the script is parented to the tool, and the tool get’s destroyed.

1 Like

Shoot, your right. I completely misread the question.

1 Like

So could this be a studio bug?

1 Like

I dont think so. Im a bit confused as to what you mean by “the old character”. If the tool is in starter pack, the local script in it shouldn’t have ran yet.

1 Like

StarterPack tools get copied to the backpack. Before your character is spawned. The backpack is inside the Player object.

For some reason, when you respawn the Player object still holds the old reference instead of it being nil.

1 Like

Anything inside the StarterPack is instantly transported to a new character when it spawns. Scripts run in the Backpack.

2 Likes

How do you know it holds the old reference?

1 Like

Should it hold the new character, instead of being nil, since the Backpack items are loaded in whenever a Character spawns?

1 Like

Because if you print the character it actually prints it, but when you print the parent it’s set to nil. That’s the old character reference. The reason I know this is because I tested it try this script and reset and put it in a tool:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

print(Character, "   Parent ", Character.Parent)

local Root = Character:WaitForChild('HumanoidRootPart')

Root.Anchored = true

As you see I do Player.CharacterAdded:Wait() or Player.Character, it doesn’t actually wait for the character to be added, that means Player.Character is not nil and the variable is set to it, making it have the old reference. (That’s how or works if the thing before it is not nil it sets it to the next thing in vairables)

1 Like

Try adding a wait before printing. The tools may be parented to the character before the character is parented to workspace.

1 Like

Yah I already did this to fix it but I think it’s pretty hacky, I would think that the character reference should be nil after the character dies.

1 Like

No. I mean add the wait after the definition, but before the wait. That way we can tell if it has the new character, it just hasnt been parented to workspace yet.

1 Like

In my opinion I think Player.Character should be nil after the character dies (And I think this is what happens but for some reason it’s not working)

1 Like

That doesn’t really change anything

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

wait()
print(Character, " Parent ", Character.Parent)

local Root = Character:WaitForChild(‘HumanoidRootPart’)

Root.Anchored = true

Like this? I did that and it still prints the old character reference, parented to nil

1 Like

Change the wait to like 10 or something to be absolutely sure.

1 Like

Nope doesn’t change anything:


https://gyazo.com/ccc7a146845735db485c5d642b6031fa

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

wait(10)
print(Character, "   Parent ", Character.Parent)

local Root = Character:WaitForChild('HumanoidRootPart')

Root.Anchored = true

This is why I think Player.Character holds the old reference before respawning. Let me test if this is true on the serverside.

1 Like

If you respawn, your character will not update itself. You will need to grab the new character model.

1 Like