Hello, I'm trying to find an alternative to "LocalPlayer" that I can use in normal scripts. Please answer if you know!
What is an alternative to "LocalPlayer" that I can use in normal scripts?
4 answers
Simply with a function.
If the function your using is touched then you can do:
if hit.Parent:FindFirstChild("Humanoid") then
local player = hit.Parent
end
- The player object passed by PlayerAdded.
game.Players.PlayerAdded:Connect(function(player)
print(player.Name) -- their name
end)
- The player object returned by GetPlayerFromCharacter() if it successfully finds a player associated with the given instance.
local player = game.Players:GetPlayerFromCharacter(object)
if player then print(player.Name) end
I'm only mentioning these two because you'll find yourself using these two the most depending on what you're doing. If you're doing other things that don't involve either of these you'll find yourself using alternatives.
One way you can access the local player in a ServerScript is to do it through a RemoteEvent.
Example: So in the case of pressing a button and updating a value on a ServerScript you will need to do:
Localscript:
script.Parent.MouseButton1Down:Connect(function() -- When button is pressed
local player = game.Players.LocalPlayer
game.ReplicatedStorage.RemoteEvent:FireServer(player) --Running an event
end)
ServerScript:
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player) --this will take the player variable from the localscript and transfer it to this function
end)
Note: this is just an example to explain how it works. Hope this helps you, have a nice day.
Since we cant use LocalPlayer in a Server script as you know we need to find another way of referencing the player.
One way is using game.Players:GetPlayers() in a for loop
aka
for ind,Player in pairs(game.Players:GetPlayers()) do
print(ind, Player.Name)
--// Note: ind is simply the position of the player in the players table its not needed normally!
-- as in print(Player.Name) will work also!
-- Do stuff with each player in here!
end
Or if your doing joined/ Left events use these
game.Players.PlayerAdded:Connect(function(Player)
print(Player.Name.." Joined the game!")
-- do stuff to the new player
end)
game.Players.PlayerRemoving:Connect(function(Player)
print(Player.Name.." Left the Game!")
-- do stuff to the new player
end)
etc...
If you know the player your dealing with as in the name of the player you can also do:
local Player_Name = "TGazza"
local LocalPlayer = game.Players:FindFirstChild(Player_Name)
if(LocalPlayer ~= nil) then
print("HI ",Player_Name,"!!")
else
print("No-one named ",Player_Name," Go away!")
end
there are other ways check the wiki for more info: https://developer.roblox.com/en-us/api-reference/class/Players
hope this helps! :)
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
Hello! I am working on a game that is similar to "Stay alive and flex your time on others". I'm trying to make a working billboard GUI above everyone's head, so that everyone can see each other's times. How could I do that with using a function?
This does not specifically find a player. This finds any rig with a Humanoid and returns it.
Oh you probably want to get all the players? game.Players:GetPlayers() returns table of all players in game, just get their characters and you've got it