Hi Guys, so I was testing this simple Kick script but it turns out an error, why it even throws error, I don’t think I wrote something wrong.
local command = "/kick"
local players = game.Players:GetPlayers()
local result
function matchPlayer(str)
for i=1, #players do
if string.find(players[i].Name, str) then
result = players[i]
end
end
return result
end
game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(msg)
if string.find(msg, command) then
print("Found")
local plrName = msg:split(" ")[2]
local player2 = matchPlayer(plrName)
player2:Kick()
end
end)
end)
Error -
ServerScriptService.Script:22: attempt to index nil with ‘Kick’ - Server - Script:22
which means no player were found with that pattern.
for an example, there’s a player named “LigmaBalls”
if you used :match("ligmaballs"), it wont match because the cases aren’t the same
Your calling GetPlayers only once when the script is first run. Any player who joins after will not be added to your current player list. Try changing the matchplayer function to this:
function matchPlayer(str)
for _, plr in ipairs(game.Players:GetPlayers()) do
if string.find(plr.Name, str) then
result = plr
end
end
return result
end