Hello, I'm trying to find an alternative to "LocalPlayer" that I can use in normal scripts. Please answer if you know!
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
game.Players.PlayerAdded:Connect(function(player) print(player.Name) -- their name end)
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! :)
Locked by DeceptiveCaster
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?