Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to use names in PlayerAdded?

local maner = {script.Value.Value}

function OnPlayerEntered(plr)
	for i = 1, #maner do
if plr.Name == maner [i] then
	plr.PlayerGui.ScreenGui.Frame.Visible = true
end end end

game.Players.PlayerAdded:connect(OnPlayerEntered)

When the player joins, if their name is on the value, the script will run.

4 answers

I'd like to point out a few things here.

Firstly, is that the server can't be accessing children of the player's PlayerGui it did not parent, and should not be accessing it at all. You should fire a RemoteEvent to the client and have them do GUI actions client-side.

Secondly, we should use the player's user ID, instead of the player's name, because the player's user ID is a unique identifier that will never chnage.

Thirdly, we can use a dictionary lookup.

local Admins = {
	[1] = "ROBLOX",
	[537472621] = "Avigant"
}

game.Players.PlayerAdded:Connect(function(Player)
	if not Admins[Player.UserId] then
		return
	end

	-- code
end)

The keys are players' user IDs, and the values are player's usernames. When we look up Admins[Player.UserId] we get a string (their username) if it is in the table, or nil otherwise.

We use a player's username as the value in the table as an easy way to see who is who when we're editing our script, as it's more useful than true or another truthey value.

Though it probably won't matter in this case, it's worth pointing out that dictionary lookup is much faster than linear search (looping through an array, looking for a matching value) as the table grows. To simplify, dictionary lookup will only ever take one step, while linear search will take an additional step for every player you add to the table.

As a final note, RBXScriptSignal:connect() is deprecated, meaning you shouldn't use it, try RBXScriptSignal:Connect() instead.

3 comments

0
  1. I need it to fire from a value 2. It needs to be names. Thank however!
1

niceee

0

You can use a RemoteEvent just how I mentioned in the first part of my post.

I am not totally for sure, as I am new to this but I am pretty sure that the server can't be accessing children of the player's "Player Gui" it did not parent, and should not be accessing it at all.Also, you should probably fire a "Remote Event" to the client and have them do GUI actions client-side. I hope this helps, again not for sure.. lol

Use a Remote Event to do this. Make sure the Remote Event is inside ReplicatedStorage.

-- Local Script anywhere that a LocalScript can be parented to.
local remote 
remote = game:GetService("ReplicatedStorage").ShowGui

remote.OnClientEvent:Connect(function()
    local plrGui
    plrGui = game:GetService("Players").LocalPlayer:WaitForChild"PlayerGui"
    
    plrGui.ScreenGui.Frame.Visible = true
end)

Now, inside the Player Added event in the Server.

-- Must be a Server Script to work.
local remote 
remote = game:GetService("ReplicatedStorage").ShowGui

game:GetService("Players").PlayerAdded:Connect(function(plr)
    if plr.Name == script.Value.Value then
        remote:FireClient(plr) -- specify which player to fire remote event to
    end
end)

If you want to get the name of the player that entered simply do this:

game.Players.PlayerAdded:connnect(function(player) --Finds the player as an Instance

if player.Name == --If the name of that instance is 

--Rest of script


1 comment

1

forgot two ends!!

This discussion is preserved as it was. New answers, comments, and votes are closed.