Unable to get a players userid from a text label

i want to get the players userid and ban them from my game even when not in my game but i get errors when getting the text from a function but not when i put the text directly

game.ReplicatedStorage.BanPlayer.OnServerInvoke = function(Admin, PlayerTBB, Reason)
	local PlayerTBBId = game.Players:GetUserIdFromNameAsync("AspectW")
	for i, v in pairs(AdminIds) do
		if Admin.UserId == v then
			print(game:GetService("Players"):GetUserIdFromNameAsync(PlayerTBB))
			Data:SetAsync(prefix .. tostring(PlayerTBBId), {
				true,
				Reason
			})
		end
	end
end

That works fine but when i do

game.ReplicatedStorage.BanPlayer.OnServerInvoke = function(Admin, PlayerTBB, Reason)
	local PlayerTBBId = game.Players:GetUserIdFromNameAsync(PlayerTBB)
	for i, v in pairs(AdminIds) do
		if Admin.UserId == v then
			print(game:GetService("Players"):GetUserIdFromNameAsync(PlayerTBB))
			Data:SetAsync(prefix .. tostring(PlayerTBBId), {
				true,
				Reason
			})
		end
	end
end

It don’t work at all can i get help as this is a little frustrating

and this is the code to invoke the server

local PlayerTBB = script.Parent.Parent.PlayerTBB.Text
local ReasonTBB = script.Parent.Parent.ReasonTBB.Text

script.Parent.MouseButton1Click:Connect(function()
    game.ReplicatedStorage.BanPlayer:InvokeServer(PlayerTBB, ReasonTBB)
end)

my error is

Players:GetUserIdFromName() failed because the user does not exist

when i use my script that dont work

You should include the error message, otherwise I don’t know what the issue is.

added it now sorry about that also if you cant seem to find it its

Players:GetUserIdFromName() failed because the user does not exist

i only get this with my broken one

1 Like

This means that PlayerTBB is undefined. Have you tried printing PlayerTBB to see if the value is ever passed through?

yh and also PlayerTBB is definatly a players name as i try RealEnzoTDZ as i have access to it and that and it prints nothing

How do you know it’s definitely defined? Do print(“PlayerTBB:”, PlayerTBB) right before the line that errors. If it’s not printing anything that means the value of script.Parent.Parent.PlayerTBB.Text is empty.

i know that its not empty as i am definitely typing in it and when i printed it before error i got nothing

That means you’re targeting the wrong value. Keep in mind that PlayerTBB is the value of the text box when the script is run. It is not updated when you change the text unless you change the value. Make PlayerTBB equal to script.Parent.Parent.PlayerTBB and when you call the function do PlayerTBB.Text. You will need to do the same for your ReasonTBB.

This might work:

game.Players.PlayerAdded:Connect(function(player)
--Rest of the code here
end)

it does get the text and i fill both text boxes they both have text in them but it just dont seem to collect that text

It doesn’t collect the text because your variable is equal to the the content of the TextBox the moment the script is run. It never updates because you’re not assigning a reference to the Text, you’re assigning the value of the Text—a string. Even if you change it, PlayerTBB/ReasonTBB will hold the original string value that was there when the script first ran which is an empty string. Please refer back to my previous answer on what you need to do.

I went through my other games to see if i could find a fix and i just didn’t index anything and it seemed to work fine.

PlayerTBB may be nil, or a username that does not exist was inputted. You should ensure:

  1. PlayerTBB is non nil
  2. PlayerTBB is a player who exists on the platform

The error you are receiving is due to the player name being inputted being incorrect. You should print out PlayerTBB and see what the server is receiving, and verify that it is a valid username.

Wrong i put the name in correctly for a fact as i copied it from the site

PlayerTBB is nil/undefined string because the moment the script is run, your variable stores the value of the TextBox at that moment in time which is before you change what’s in the text box. That’s the issue with your code.


Be sure when marking a solution, it has details on the actual solution. Not a vague “never mind, I fixed it” because people may look at this post in the future and not know what’s wrong and how to fix it.

Actually, that is not wrong at all, because you are inputting an empty string, as said above. An empty string is not a valid player name, it’s just an empty string. Like I said before, you should print the name the server is receiving, which you did, and you got an empty string as the output. I was describing to you what the error message meant because it seems there’s a misunderstanding and I wanted to be clear. If you do not debug error messages you’re getting, you will get nowhere. Please read documentation, and at least try to interpret the error before creating a post and assuming that the error message is incorrect, otherwise you won’t be learning anything new.

Additionally, users have posted a ton of advice, and already gave you the solution. You should mark @m0mmapoIar’s reply as the solution because they gave you the correct solution, as well as worked through things with you.

1 Like

Thanks about that i have now done it thanks for the reminder