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
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.
There are several different ways, but none as simple as with LocalScripts.
PlayerAdded
events 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 PlayerGui
and the second parent is Player
.
But still, use a local script!