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

How do you get the player from a script?

Asked by 10 years ago

How can you get the 'player' from a script , not a local script

for example on how the local script gets its player

local player = game.Players.LocalPlayer

but i need to know how to get the player from a script

NewTooScripting

2 answers

Log in to vote
0
Answered by 10 years ago

Well for one thing you could just add a string value called "Player" inside the script and do

repeat wait() until script.Player.Value ~= ""
local player = game.Players:FindFirstChild(script.Player.Value)

and have a local script inside the value that does

script.Parent.Value = game.Players.LocalPlayer.Name

If you didn't mean how to get LocalPlayer via Script vs. LocalScript then I will need a further explanation, otherwise this should work. There is no real way to get LocalPlayer other than doing script.Parent.Parent.Parent etc until you reach player or doing this method.

0
Why was my post down-voted? I hate all these trolls that randomly down-vote posts... I once had someone literally go through my profile and downvote every single thing they could possibly find and made my rank drop.. VariadicFunction 335 — 10y
Ad
Log in to vote
1
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

There are several different ways, but none as simple as with LocalScripts.

PlayerAddedevents have a built in parameter equal to the player who joined the game, so that's one way.

game.Players.PlayerAdded:connect(function(player)
    print(player)
end)

If you have a way to get the character, like in a touched event for example, you can use the GetPlayerFromCharacter() method.

script.Parent.Touched:connect(function(hit)
    print(game.Players:GetPlayerFromCharacter(hit.Parent))
end)

If you use a ClickDetector, you can use its built in parameter equal to the player who clicked.

script.Parent.ClickDetector.MouseClick:connect(function(player)
    print(player)
end)

If you're using a Tool, HopperBin, or GUI you should be using a LocalScript, but it is possible to 'Parent' up to the player. For example, if you have a server script in StarterGui (even though you should use a LocalScript!) and you do,

print(script.Parent.Parent)

It will print the player, since the first parent is PlayerGuiand the second parent is Player. But still, use a local script!

Answer this question