How do I get a players username into a local?

Hello,
I’ve been working on a project and I require to get the username of a player that joins into a local of a server sided script. I tried doing this:


But it returns this error:
image
How should I do this?

1 Like

'Ello there!

Do you mind elaborating on what you are looking to achieve?

1 Like

Yes, I’m making a GUI that only people in a group can access.

1 Like

Ah, alright. Mind showing the current code that you have written and any errors?

1 Like

Sure, but I must say there are many scripts in it including ModuleScripts and LocalScripts.

This is the only error: b55f00269e8737e1f3d806d60ee56dea127f7e19

1 Like

Try removing the currentUser variable? Unless you need to disconnect it later.

Alright, I’ll see if that works.

2 Likes

Alright, mind showing your current code?

The current whitelist:


The MainModule: TwitLonger — When you talk too much for Twitter

1 Like

Well, not sure if you can make a function a variable like that. Instead, make a local function, return the players name. Then make the function a variable. Also, it would be easier to just do the PlayerAdded event separate.

Alright, how would I make the variable function in the local script, though?

1 Like

You should rewrite this all into one function to handle one user per player connection.

I could, however I plan to make this into a loadable ModuleScript with require().

Here is the entire tree of the GUI so far:
image

1 Like

local function FetchPlayerName(Player)
return Player.Name
end

local PlayerName = FetchPlayerName()

game.Players.PlayerAdded:Connect(FetchPlayerName)

print(PlayerName)

OR:

game.Players.PlayerAdded:Connect(function(Player)
print(Player.Name)
end)

(May have messed something up, writing at 1am on mobile).

This would usually be a great solution, but it does not work with my original plan, to find out the username of a player using the local element. It needs to work with this:
image

1 Like

You could just check the if the player is in the group as they join.

I’ll try doing that, thank you.

1 Like

Here’s a simple solution to setting a “CurrentUser” for the script.

--// Services
local Players = game:GetService("Players")

--// Variables
local CurrentUser

--// Functions
--// When a new player is added to the server
local function OnPlayerAdded(Player)
	CurrentUser = Player
end

--// Call the OnPlayerAdded function
Players.PlayerAdded:Connect(OnPlayerAdded)

Hey!

local CurrentUsers = {}
local GroupId = 0000
local Bindable = Instance.new("BindableEvent") 

game:GetService("Players").PlayerAdded:Connect(function(Player) 
   CurrentUsers[Player.Name] = {CheckInGroup = false}
   Bindable:Fire("GroupCheck") 
end) 

Bindable.Event:Connect(function(args) 
if args == "GroupCheck" then
   for i,v in pairs(CurrentUsers) do
       if v["CheckInGroup"] == false and game:GetService("Players"):FindFirstChild(i):IsInGroup(GroupId) then
            v["CheckInGroup"] = true
            end
        end
    end
end) 


game:GetService("Players").PlayerRemoving:Connect(function(Player) 
    if CurrentUsers[Player.Name] then
        CurrentUsers[Player.Name] = nil
    end
end