Hi, I need to make a script for my game that performs the following functionality:
Write to chat → /kick “playername” → the player has been kicked
How can I do that? I’ve been looking for a couple of hours and can’t find anything
Hi, I need to make a script for my game that performs the following functionality:
Write to chat → /kick “playername” → the player has been kicked
How can I do that? I’ve been looking for a couple of hours and can’t find anything
Hey there, Matias! I would start off by running a script where when the player joins, it activates the script. So:
game.Players.PlayerAdded:Connect(function(player)
Then, roblox has something called player.Chatted, and passes a parameter of what the player says.
player.Chatted:Connect(function(message)
What I would do is turn this message, and lowercase everything so that it’s not case sensitive.
officialMessage = message:lower()
Then, you are able to use the message that the player took, and use the string.find(message, “/kick”) function, and look for within your string for /kick. If the /kick is present, split your string by whitespaces, take the 2nd position of your table, iterate through your players and then use the :kick function to kick them.
I hope this helps!
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function()
plr:Kick()
end)
end)
You can pick up it as base for your functionality.
thanks you! it is good?
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if msg == "hello" then
plr:Kick()
end
end)
end)
It will kick anybody who says hello… take a look at mine.
yes, it would work, you can write it.
My guy. It would kick anybody who says “hello”.
Oh perfect! Now I understand how this works, thank you guys very much! now I’m going to improve that code and implement it to my game as it really should be
i guess he wants to kick anybody with this message so whats the problem?
There you go! Happy coding! If you need any help let us know.
If you hook it to a player:Chatted event, you could do something like:
-- do some checking and whatever to make sure they're an admin
-- replace anything you need to if it doesn't break it
local Prefix = '/'
Player.Chatted:Connect(function(msg)
local splitString = string.split(msg, " ") -- find spaces in text showing <command, target> layout
if splitString[1] and splitString[2] then
local stringA = splitString[1]
if stringA ~= Prefix then return end
local targetName = splitString[2]
local plr = game:GetService("Players"):FindFirstChild(targetName)
if plr then
plr:Kick("Kicked from this game.")
end
end
end)
oh lol i’m stupid i just noticed he wants to do it by he’s own, not automaticly, sry lol
What about this code I did? Would I kick if I write /kick and the player’s name?
game.Players.PlayerAdded:Connect(function(plr)
plr.Chatted:Connect(function(msg)
if msg == "/kick "..plr.Name then
plr:Kick()
end
end)
end)
That can only kick yourself.
You need string.split to get a target.
**ServerScript: **
Please note that you need to insert your userid into the table for it to work.
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
local newMsg, replaced = string.gsub(str, "/kick ", "") --take the message and remove the "/kick " part
for I, v in pairs(game.Players:GetChildren()) do --loop through players
if v.Name:lower() == newMsg:lower() or 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)
Spot on. Use this script up here. That’s what I tried to say ^
Unless he wants normal players to let have access to kick command.
“and admins[player.UserId] then”
Spot off. What I mean is that if he wants literally everyone to have access to the kick command, there is no need for the admin table.
I don’t think he’d want to give everybody a kick command… sounds a bit abusive.