The textbox’s text is the user id of the player I want to ban. here is my code:
local banUserID = script.Parent.Frame.BanUserID
local id = banUserID.UserId.Text
local fd = tonumber(id)
banUserID.Ban.MouseButton1Click:Connect(function()
local plrToBan = game:GetService("Players"):GetPlayerByUserId(fd)
plrToBan:Kick("haha get banned")
end)
The reason this is erroring is because fd is being set to the text of the TextBox, which is empty when the script starts and you don’t update the variable after the TextBox content has changed. tonumber returns nil if there is no valid number. For example, tonumber("string") returns nil because there is no number, and that’s what’s happened in this script, fd is tonumber(""), so it’s nil.
It’s also worth mentioning that because this should be in a client script you shouldn’t be kicking in the same script, you should be using a remote event. You can read up on them here, they help bridge the gap between client and server. Bindable Events and Functions | Roblox Creator Documentation
RemoteEvent.OnServerEvent:Connect(function(Player, Id)
local fd = tonumber(id)
local plrToBan = game:GetService("Players"):GetPlayerByUserId(fd)
plrToBan:Kick("haha get banned")
end)
I tried your solution, it did not work. Also I know that Kick should be done in another script, its just that im testing this ban player by user id system
This is the solution that I hinted at if you were to keep everything in one script, is it what you tried? Redefining the variables after the click. It’s probably worth your time to add a couple of if statements in there as well, I’ve added an example one in there for you:
local banUserID = script.Parent.Frame.BanUserID
banUserID.Ban.MouseButton1Click:Connect(function()
local id = banUserID.UserId.Text
local fd = tonumber(id)
if fd then
local plrToBan = game:GetService("Players"):GetPlayerByUserId(fd)
plrToBan:Kick("haha get banned")
else
banUserID.UserId.Text = "Please insert a valid number!"
end
end)