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!
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)
There is no actual way. You can use events like Leamir said game.Players.PlayerAdded
.
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.