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.
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:
@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?
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.
-- 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)
--]]