Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
3

How to make get Localplayer in a serverscript?

Asked by 4 months ago
Edited 4 months ago
local plr = game.Players.Localplayer

lets say i put a serverscript in the workspace and want to get Localplayer. But the problem is that it doesnt work. I saw articles about this but i dont understand how to solve it. Please help.

Edit : I want to use it like a variable so i can get the players backpack

Edit : To get a player just add another function

game.Players.PlayerAdded:Connect(function(player)

and remember to add another end) if you have two functions!

0
You cant get local player, but you can use game.Players.PlayerAdded event to get the player Leamir 2963 — 4mo
0
You can't get the LocalPlayer by a variable, you can get it by doing: game.Players.PlayerAdded:Connect(function(player), but you can get it with a variable doing this: local player = script.Parent.Parent.Parent only if it's something in your backpack like a Tool for example because Tool's parent changes everytime the Parent is changed. LetalNightmare 99 — 4mo
0
a server holds all clients, which means even if there was a variable to get the player, it would get every player or a server instanced as a player which wouldnt make sense, you can however stole player names in a table and create a variable for each player to index them... greatneil80 2350 — 4mo

3 answers

Log in to vote
2
Answered by 4 months ago
Edited 4 months ago

I run you through a couple of ways to get the player

Local Script:

local player = game.Players.LocalPlayer 
-- Only one needed



Server Script/ Normal Script:

game.Players.PlayerAdded:Connect(function(player)
end)

-- If it's in a gui
local gui script.Parent.Parent.Parent.Parent -- depends on how many layers it has

-- If touched a part

local function onTouched(part)
    local player = game.Players:GetPlayerFromCharacter(part.Parent)

-- remote events(Which switches from a different type of script e.g local to server or other way 
round)

-- This is a remote event example(This is alot more complicated)
game.ReplicatedStorage.RemoteEvent:FireServer(player)

--receiving script
game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(player)

0
why send player? it is aways sent client->server Leamir 2963 — 4mo
0
This is a solid answer! Nice! Roger111 332 — 4mo
Ad
Log in to vote
2
Answered by 4 months ago

There is no actual way. You can use events like Leamir said game.Players.PlayerAdded.

Log in to vote
0
Answered by 4 months ago

You can't get the LocalPlayer by a variable, you can get it by doing: game.Players.PlayerAdded:Connect(function(player), but you can get it with a variable doing this: local player = script.Parent.Parent.Parent only if it's something in your backpack like a Tool for example because Tool's parent changes everytime the Parent is changed.

Answer this question