为什么我的脚本不能踢用户



这个脚本是这样设计的,当你说"!踢[用户]";它将该用户踢出游戏。但是,当我测试它时,输出框中没有任何错误,什么都不会发生。发生了什么事?

game.Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(player,message)
if player.Name == "playername" then
local words = string.split(message," ")
if string.lower(words[1]) == "!kick" then
words[2]:Kick("You have been kicked.")
end
end
end)
end)

感谢

现在什么都没有发生的原因是因为您的第一次检查失败了。你有播放器的参数。向后聊天连接。邮件排在第一位,收件人排在第二位。因此,您命名为player的实际上是消息,而if player.Name == "playername" then可能会失败,因为字符串没有Name属性,player.Name为nil。

之后,您将遇到的下一个问题是,将命令拆分为两部分后,命令的后半部分仍然是字符串,而不是Player。因此,您需要找到一个具有匹配名称作为输入字符串的Player。

试试这样的东西:

local Players = game.Players
local function findPlayerByName(name)
-- escape if nothing is provided
if name == nil then
return nil
end
-- check if any players match the name provided
local allPlayers = Players:GetPlayers()
for _, player in ipairs(allPlayers) do
if player.Name == name or string.lower(player.Name) == name then
return player
end
end
-- couldn't find any players that matched
return nil
end
-- define all the players that can use commands
local admins = {
["playername"] = true,
}
Players.PlayerAdded:Connect(function(player)
player.Chatted:Connect(function(message, recipient)
if admins[player.Name] then
local words = string.split(message, " ")
local cmd = string.lower(words[1])
if cmd == "!kick" then
local playerName = words[2]
local targetPlayer = findPlayerByName(playerName)
if targetPlayer then
targetPlayer:Kick("You have been kicked.")
end
end
end
end)
end)

最新更新