Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

What is an alternative to "LocalPlayer" that I can use in normal scripts? [closed]

Asked by 4 years ago

Hello, I'm trying to find an alternative to "LocalPlayer" that I can use in normal scripts. Please answer if you know!

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?

4 answers

Log in to vote
0
Answered by 4 years ago

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

0
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? KreativleV2 0 — 4y
0
This does not specifically find a player. This finds any rig with a Humanoid and returns it. DeceptiveCaster 3761 — 4y
0
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 imKirda 4491 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
  • 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.

Log in to vote
0
Answered by
Ghost40Z 118
4 years ago
Edited 4 years ago

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.

Log in to vote
0
Answered by
TGazza 1336 Moderation Voter
4 years ago

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! :)