Username is not a valid member of Players

Getting Username is not a valid member of Players error.

local username = script.Parent.Text
game.Players.PlayerAdded:Connect(function(plr)
return plr
end)

script.Parent.MouseButton1Click:Connect(function()
wait(1)
local therealplayer = game.Players[username]
therealplayer:Kick("You died!")
end)

Why does the PlayerAdded function return plr?

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 text button’s text is a players name and the player added was something I tried.

Try adding print(username) after the wait(1).

and the correct error is
KreedKernmagne is not a valid member of 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

2 Likes

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.

Hope this helps! :smiley:

1 Like
local player = game.Players:FindFirstChild(script.Parent.Text)
if player then

So this if statement is not executing (put a print) and the username is correct.

iirc, game.Players.username will literally look for a player called “username”.

1 Like

Yes good point, use “[ ]” then in this case.

1 Like

What exactly are you trying to make anyway?
A kick gui?

Yes, in a sense. Its for my mafia game.

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

So yeah, use FocusLost().

LocalScript for GUI:

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 :sweat_smile:
There may be some errors in this, reply to me if you get any,

player and plr_kick are never referenced

As I said the first parameter will always just be the player, you can name it orange and it would still pass

*Edit: Yes sorry I didn’t realise I hadn’t referenced it so just pass the LocalPlayer instead

It just says unknown global for the local script.

Change it to game.Players.LocalPlayer then

FireServer automatically passes the player firing it - you do not need to declare it.

1 Like

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

2 Likes