CharacterAdded:Wait() not working

I have a regular script inside of a tool used by the player. The tool loads as soon as the game starts, and it is inside their backpack. It looks for the local player and the Character at the start of the script, but it is not working.

player = game:GetService("Players").LocalPlayer
Character = player.Character or player.CharacterAdded:Wait()

Is there something I can do to allow it to work? I’ve tried adding a wait() at the very top of the script but the output still gives me an error with "Attempt to index nil with ‘Character’.

2 Likes
player = game:GetService("Players").LocalPlayer
player.CharacterAdded:Wait()
Character = player.Character

Does this solve your problem? It may be because you’re telling it to index a non-existent character before telling it to wait.

1 Like

You said that you have e regular script inside the tool? If yes then you have to know that you can’t get the local player from a regular script, use a local script instead

2 Likes

Unfortunately, no. Instead it gives me an output of "attempt to index nil with ‘CharacterAdded’

1 Like

This is probably the reason, good catch. I misread. .LocalPlayer is only accessible from a LocalScript. If you need to use a normal script, find a different method of referencing the player or change it to a LocalScript instead.

1 Like

How would I be able to reference the player from a normal script?

If the tool is something you’re putting in the player’s backpack once the game starts, you can reference them using that hierarchy.
image

local player = script.Parent.Parent
player.CharacterAdded:Wait()
local Character = player.Character
1 Like

Success! I altered the code a little bit for it to be “script.Parent.Parent.Parent” so it could address the player instead of the backpack. Script seems to work now.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.