LUA - 如何停止脚本运行

你好,我正在制作一个roblox LUA脚本,我无法弄清楚如何在if语句之后停止脚本运行。以下是我的代码:

if not newPlayer:GetRankInGroup(workspace.CoreValues.GroupID.Value) then
    teamName = "Civilian"
    (STOP)
end

原文链接 https://stackoverflow.com/questions/69997500

点赞
stackoverflow用户2860267
stackoverflow用户2860267

根据您编写代码的结构,您可以简单地 return

local shouldEscape = true
if shouldEscape then
    return
end
print("This line won't get hit")

但是,如果您已经设置了事件侦听器,这将无法阻止它们触发。您需要清理这些事件侦听器、禁用脚本或删除脚本。

-- 以连接信号为例
local connection = game.Players.PlayerAdded:Connect(function(player)
    -- 如果脚本仍然启用,这行将被打印
    print("Player Added : " .. player.Name)
end

local shouldEscape = true
if shouldEscape then
    -- 禁用脚本
    script.Disabled = true

    -- 或:断开信号
    --connection:Disconnect()

    -- 或:完全删除脚本
    --script:Destroy()

    -- 退出以防止执行其他代码
    return
end
print("This line won't get hit")
2021-11-17 00:54:59