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:

How should I do this?
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:
'Ello there!
Do you mind elaborating on what you are looking to achieve?
Yes, I’m making a GUI that only people in a group can access.
Ah, alright. Mind showing the current code that you have written and any errors?
Sure, but I must say there are many scripts in it including ModuleScripts and LocalScripts.
This is the only error:
Try removing the currentUser
variable? Unless you need to disconnect it later.
Alright, I’ll see if that works.
Alright, mind showing your current code?
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?
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:
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:
You could just check the if the player is in the group as they join.
I’ll try doing that, thank you.
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