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

How do you find the LocalPlayer in a regular script?

Asked by
sad_eyez 162
8 years ago

I am making a surfacegui that clones a musicid player into the playergui, but I need to know how to find the localplayer in a regular script, if you can maybe leave various ways to do it, so I can test them out and not have to ask anymore, thanks for the help if you can, heres my code I need it for though:

script.Parent.MouseButton1Click:connect(function()
    local musicgui = game.ReplicatedStorage["MusicID"]:Clone()
    musicgui.Parent = plr.PlayerGui
end)

1 answer

Log in to vote
1
Answered by 8 years ago

You don't
Well, technically.

You don't find the 'Local Player', but instead you find the Player that triggered an event. In the case of SurfaceGuis, you have to place the SurfaceGui inside of the Player's PlayerGui in order to get the Player that did anything with its contents.

In order to do this, you'll want the SurfaceGui inside Player.PlayerGui (Presumably Player here comes from PlayerAdded or Players:GetChildren()), and the Adornee property set to the Part you wanted it to be on.

Then, to find the Player using that setup, you can either use SurfaceGui.Parent.Parent, or if you want a fire-and-forget thing to do it for scripts inside of the SurfaceGui (Or its descendants), then you can use this:

local Player = script;
repeat Player = Player.Parent until Player.Parent == game.Players;
-- Player is now your Player who has the Gui
Ad

Answer this question