How do i find the player in a serverscript?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    i want to create a variable for a viewportframe inside of a gui inside of the playergui with a local script
  2. What is the issue? Include screenshots / videos if possible!
    it only works cause i typed my username in
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i looked on the dev hub but it all uses functions instead
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
    part of the script
local players = game:GetService("Players")
local playergui = players:WaitForChild("ALEXS12345678910").PlayerGui.MainMenuGUI.MainMenu.SkinsAndGearFrame.MainViewportFrame
local viewport = playergui

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

3 Likes

It is better to change playergui from a local script, and if you want the player in a serverscript, you would use remote events for that.

i need to parent an object into a viewport frame though

Any reason you can’t do that with local script?

3 Likes

i dont need a server script to parent stuff from replicated storage?

1 Like

no, thats only for serverstorage

oh ok thanks ill try it that out then

1 Like
local players = game:GetService("Players")
local selfCharacter = workspace:FindFirstChild("ALEXS12345678910")

if selfCharacter then
	local selfPlayer = players:GetPlayerFromCharacter(selfCharacter)
	if selfPlayer then
		local selfPlrGui = selfPlayer:FindFirstChild("PlayerGui")
		if selfPlrGui then
			local selfGuiObject = selfPlrGui.MainMenuGUI.MainMenu.SkinsAndGearFrame.MainViewportFrame
		end
	end
end

Do you want this to only work for you or all players? As the above post suggested this should be handled from the client using a local script, you can reference the ViewportFrame & BasePart instance correctly from a local script, the script doesn’t have to be parented to either.

1 Like

You can find a player by using the PlayerAdded event. If you are trying to look through all players, use a loop like this:

local Players = game.Players:GetPlayers()

for i = 1, #Players do
    local Player = Players[i]
    -- Do whatever there.
end

Just following this quote:

local players = game:GetService("Players")
local playergui
local viewport

players.PlayerAdded:Connect(function(plr)
if plr.Name == "your name here" then
playergui = plr:WaitForChild("PlayerGui"):WaitForChild("MainMenuGUI").MainMenu.SkinsAndGearFrame.MainViewportFrame
viewport = playergui
end
end)

I’m not entirely sure what you wanted, though. Could you please elaborate a bit more?

Off-topic, but would that work? I’ve never seen that before, but I’m used to this:

for _, v in pairs(game.Players:GetChildren()) do
    blah blah blah
end

Yep! It is actually a more simple way of doing that. It is more efficient.

Oh, okay, thanks. Do you know how it is more efficient, though?

Fewer lines of code. Also, it uses an Index instead of a variable, which is better, because instead of referencing a player object, it literally gets the player object.

1 Like

I could have worded that better, but I recon you use this version instead of yours!

1 Like