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)
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.
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.
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.
PlayerTBB may be nil, or a username that does not exist was inputted. You should ensure:
PlayerTBB is non nil
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.
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.