Is the script’s parent a TextButton or a TextBox? MouseButton1Click is a button event, but only a TextBox’s Text property can be changed by the player without using scripts.
The issue is probably that your script is looking for someone named “TextButton” inside game.Players.
The variable username is initiated at the beginning of the script and will remain that way, so at first, its text was “Username”, it will be recorded in the variable and will remain constant. Updating the variable or just passing the current text as an argument will work
There are some other things wrong in your script too:
First off, you should make sure that the name that the client inputted is for an actual player in the game, because they could misspell the player’s name and not consider the cases of the letters. You can use FindFirstChild to make sure the player exists
Second of all, kicking other players won’t work on one player’s client - they can only kick themselves. The solution would be to use a RemoteEvent, where it’ll be handled on the server and you will then fire it on the client to kick the player.
Here’s what the scripts should look like:
Local script
local RE = game.ReplicatedStorage:WaitForChild("KickEvent")
script.Parent.MouseButton1Click:Connect(function()
wait(1)
local player = game.Players:FindFirstChild(script.Parent.Text)
if player then
RE:FireServer(player)
end
end)
Server script (in ServerScriptService)
local RE = game.ReplicatedStorage:WaitForChild("KickEvent")
RE.OnServerEvent:Connect(function(player, name)
local p = game.Players:FindFirstChild(name)
if p then
p:Kick()
end
end)
I’d also like to ask why is there a PlayerAdded event there that does nothing but return the player that joined? Also, with the RemoteEvent, you should perform checks to properly secure it because exploiters can call the event and kick everyone in the server
Because most likely the player your trying to get will not have loaded in, there are multiple reasons for why this isn’t working so let me suggest some solutions:
Add a wait() delay of about 3+ seconds at the top.
If you are trying to input text to kick a player which is what I assume your doing, it wont work because the text won’t save in-game, you’d most likely need to fire an event and pass the player’s name as a parameter. Try using TextBox.FocusLost for that.
Since most usernames won’t have spaces now a day, use game.Players.username.
You are returning plr for no reason as it doesn’t affect anything else in your script. If you are getting the username value from a Text field, there is no need for a PlayerAdded event.
You should always check the output for errors and ensure that you are typing the name EXACTLY how it is. Write prints to ensure that the condition of the if statement is met. Say you want to check if the username is correct:
print(player.Name == script.Parent.Text)
This will print true, if they are equal, otherwise it’ll print false
local PlayerToKick = GUI.textbox -- change to path to your textbox
local Button = script.Parent -- assuming this is button, change accordingly once again
local Event = game:GetService("ReplicatedStorage").RemoteEvent -- make one, it's neccessary
Button.MouseButton1Click:Connect(function()
Event:FireServer(player, plr_kick) -- In a remote, first param will always be player who fired.
end)
Server Script:
local Event = game:GetService("ReplicatedStorage").RemoteEvent
Event.OnServerEvent:Connect(function(player, plr_kick)
game.Players:WaitForChild(plr_kick):Kick("Reason here") -- this kicks player specified
-- if name was wrong, will return an error
end)
Yes I didn’t use FocusLost in this because you have a button, scratch what I said about that
There may be some errors in this, reply to me if you get any,
You don’t need to pass the player as an argument in FireServer, because the player who fired the event is always the first parameter in OnServerEvent, so you’ll have to exclude the player from it.
You shouldn’t use WaitForChild to get the player, and WaitForChild technically doesn’t throw an error, it makes a warning in the output and will yield indefinitely until the player exists