Attempt to Index nil with "CharacterAdded"

local datastore = game:GetService("DataStoreService")

local players = game:GetService("Players")

local player = players.LocalPlayer

players.PlayerAdded:Connect(SetText())

player.CharacterAdded:Connect(SetText())

Does anyone know why this code is returning the error in the title? I’m probably missing something very silly, any help is appreciated.

LocalPlayer can only be used in LocalScripts, on the server its nil

2 Likes

LocalPlayer is nil on the server. You’d want to do something like this:

local datastore = game:GetService("DataStoreService")
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
    SetText(player)
    player.CharacterAdded:Connect(SetText)
end)
1 Like

Players.LocalPlayer only works for LocalScripts, not server scripts, as well as inside the connection you run the function instead just remove the (), it would look something like this:

local datastore = game:GetService("DataStoreService")

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
   SetText(player)
   player.CharacterAdded:Connect(SetText)
end)

Note, that the function will either receive the character instance or the player instance, I do not think this was intended so instead do the following if you only want to send the character instance:

players.PlayerAdded:Connect(function(player)
   player.CharacterAdded:Connect(SetText)
end)
2 Likes

Thanks for the help.

@HugeCoolboy2007 @KJry_s Why does putting “player” in the function parenthesis work that way? How does it know it’s not just some other variable being passed into the function?

are you trying to get LocalPlayer in Server context?
In that case, you’ll get a nil, because you can’t get LocalPlayer from server script.

You need to get it from the Players service like

players[plr.Name]
1 Like

Yes, I’m looking for the local in server context.

By default, Players.PlayerAdded always returns the player who joined the game. Also, it depends on how your function is being handled. For example, if you’re doing something like this:

local function myFunction(arg)
   print(arg.Name)
end

Since Name is a property of both the Player and Character, your function dynamically fixes itself and you wouldn’t have to worry about errors. However, if you do:

local function myFunction(arg)
   print(arg.Name)
end

myFunction(10) 

This will error because a number doesn’t have a Name property.


https://developer.roblox.com/en-us/api-reference/event/Players/PlayerAdded
https://developer.roblox.com/en-us/api-reference/event/Player/CharacterAdded
https://developer.roblox.com/en-us/api-reference/class/Player

-- an extra example
local function myFunction(arg)
   print(arg.Name)
end

Players.PlayerAdded:Connect(myFunction)

--[[ this is the equivalent of
   Players.PlayerAdded:Connect(function(player)
       myFunction(player)
  end)
--]]
1 Like
players.PlayerAdded:Connect(function(player)

SetText()

player.CharacterAdded:Connect(SetText())

end)

When I run this I get an error saying “Attempt to connect failed: passed value is not a function”, any idea why it may be doing this?

When connecting functions to events, only type the function name. The compiler thinks you’re trying to pass the value returned from the function:

players.PlayerAdded:Connect(function(player)

SetText()

player.CharacterAdded:Connect(SetText)

end)
1 Like