Help with chat message script

what about this? or am i wrong? :frowning:

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:Connect(function(msg)
		
		local player = game.Players:GetPlayerByUserId()
		
		if msg == "/kick ".. player.UserId then
			plr:Kick()
		end	
	end)
end)

That will kick the player that chatted the message. Is that what you want, or do you want to kick other players?

I need to write the name of the player and if it is the same as an online player then I kicked it
like this: /kick playerName

That code won’t work because player will return nil since you didn’t put anything into GetPlayerByUserId function.

This would only kick yourself. You need to iterate through the players. Once again, this would give everybody access to kick somebody. I would use a for i,players in pairs(game.Players:GetPlayers()) do loop. Then you would say:

if players.UserId == msg[2] then – assuming you split the message.
players:Kick()
end

Try the code that I provided, it should work.

2 Likes

Facts. His code will work. Trust the process. ^

1 Like

what’s this part?
(str, "/kick ", "")
should I change something about that part?

So the goal of that is to make a variable named “newMsg”, that is the string - the “/kick” part. GSub is used to take out part of a string. On further thought, I don’t actually think you need that part of the code. I’ll change my code rq and repost it.

No, it takes the prefix and deletes it, then you’re left with only the player you want to kick. The prefix activates it, the removal of the prefix gets you the player.

The following code would kick me, if you chatted “/kick xdjackyboiixd21”.

local admins = {

12345678,
987654321,
}  --table with the UserIds of all the admin players



game.Players.PlayerAdded:Connect(function(player)
      player.Chatted:Connect(function(msg) --when they chat
             if msg:match("/kick") and admins[player.UserId] then -- check if the message includes "/kick", and check if they are in the admin table
                     for I, v in pairs(game.Players:GetChildren()) do --loop through players
                        if msg:lower():match(v.Name:lower()) then --check if there name is in the message
                          v:Kick() --kick them
                          break --end the loop
                        end
                     end
             end
      end)
end)
1 Like

What happens if more players join and I put their name, I can also kick them or only the id that are in the table?

The admin table doesn’t have to do with the kicking part, it is only to make sure a normal player cannot kick another player. Only player’s who’s Ids are int he table can kick other player’s, so anybody who you want to give the Admin controls, just put their UserId into the table. Then to kick a player, just type “/kick MatiasHarders”.

1 Like

Thank you so much Jack! really thank you very much, i will implement to my game now
I get the id in my roblox profile, right?

Yes, you get the ID from the roblox page. Your id is 1739689185, so the table should look like this:

local admins = {
1739689185
}

Now I have never tested this, so if it doesn’t work just let me know and I can try to fix it. Also if you are creating a server in studio to test it, you can just use this code to test it:

game.Players.PlayerAdded:Connect(function(player)
      player.Chatted:Connect(function(msg) --when they chat
             if msg:match("/kick") then -- check if the message includes "/kick"
                     for I, v in pairs(game.Players:GetChildren()) do --loop through players
                        if msg:lower():match(v.Name:lower()) then --check if there name is in the message
                          v:Kick() --kick them
                          break --end the loop
                        end
                     end
             end
      end)
end)
1 Like

I have been testing and the admin table does not work, anyone who enters can use the command, how can I make it enable only for those who are admin? :frowning:

on your 3rd line, you have to add "and admins[player.UserId]… this line checks to see if the admin’s ID is located in both the table and matches the player who said the command.

The new code that I did is just for testing sake. For the real game you want to use this code:

local admins = {
1739689185
}  --table with the UserIds of all the admin players



game.Players.PlayerAdded:Connect(function(player)
      player.Chatted:Connect(function(msg) --when they chat
             if msg:match("/kick") and admins[player.UserId] then -- check if the message includes "/kick", and check if they are in the admin table
                     for I, v in pairs(game.Players:GetChildren()) do --loop through players
                        if msg:lower():match(v.Name:lower()) then --check if there name is in the message
                          v:Kick() --kick them
                          break --end the loop
                        end
                     end
             end
      end)
end)

i tried it and nothing happen, can’t kick :frowning:

Alright let me test this code on my computer, i’ll get back to you when I have a fix.

1 Like