You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Roblox Lua脚本问题:if条件为假时Teleport函数仍触发

Roblox脚本触发逻辑问题排查

原代码

local Players = game:GetService("Players")
local TouchPart = workspace["Start"] 
local target = game.Workspace.Level1

local function teleport()
    TouchPart.Touched:Connect(function(touched)
        if touched.Parent:FindFirstChild("Humanoid") then
            local Player = Players:GetPlayerFromCharacter(touched.Parent)
            if Player then
                    --Player.Character.HumanoidRootPart.CFrame = target.CFrame + Vector3.new(0, 5, 0)
                    print("Teleport")
            end
        end
    end)
end

while true do
    local zeit = game.Workspace.wall1.time.Value
    zeit = zeit - 1
    game.Workspace.wall1.time.Value = zeit
    game.Workspace.wall1.SurfaceGui.Frame.TextLabel.Text = zeit
    wait(1)
    print(zeit)
    if zeit == 0 then
        teleport()
        game.Workspace.wall1.time.Value = 20
    end
end

预期与问题

  • 预期逻辑:仅当zeit等于0时,玩家触碰TouchPart才会打印"Teleport";非0状态下触碰无反应。
  • 遇到的问题:即使zeit不为0,玩家触碰部件仍会触发打印。

问题原因

每次zeit归0时调用teleport(),都会给TouchPart.Touched事件新增一个永久连接——这些连接不会自动消失,哪怕后续zeit被重置为20,之前绑定的回调依然会在触碰时执行。循环多次后,部件上会绑定多个相同的触发逻辑,只要触碰就会触发所有已绑定的回调,自然会持续打印。

修复方案

有两种简洁的修复方式,按需选择:

方案1:事件绑定始终存在,回调内判断zeit状态

不需要每次zeit归0才绑定事件,直接在脚本初始化时绑定一次,触发时判断当前zeit是否为0即可:

local Players = game:GetService("Players")
local TouchPart = workspace["Start"] 
local target = game.Workspace.Level1
local timeValue = game.Workspace.wall1.time

-- 初始化时绑定一次事件
TouchPart.Touched:Connect(function(touched)
    -- 触发时先判断zeit是否为0
    if timeValue.Value ~= 0 then
        return
    end
    if touched.Parent:FindFirstChild("Humanoid") then
        local Player = Players:GetPlayerFromCharacter(touched.Parent)
        if Player then
            print("Teleport")
            -- Player.Character.HumanoidRootPart.CFrame = target.CFrame + Vector3.new(0, 5, 0)
        end
    end
end)

while true do
    local zeit = timeValue.Value
    zeit = zeit - 1
    timeValue.Value = zeit
    game.Workspace.wall1.SurfaceGui.Frame.TextLabel.Text = zeit
    wait(1)
    print(zeit)
    if zeit == 0 then
        timeValue.Value = 20
    end
end

方案2:管理事件连接,重置时断开旧连接

如果需要严格控制事件的绑定/解绑时机,可以保存连接对象,在zeit重置时断开旧连接:

local Players = game:GetService("Players")
local TouchPart = workspace["Start"] 
local target = game.Workspace.Level1
local timeValue = game.Workspace.wall1.time
local touchConnection = nil -- 保存事件连接对象

local function teleport()
    -- 绑定前先确保没有旧连接
    if touchConnection then
        touchConnection:Disconnect()
    end
    touchConnection = TouchPart.Touched:Connect(function(touched)
        if touched.Parent:FindFirstChild("Humanoid") then
            local Player = Players:GetPlayerFromCharacter(touched.Parent)
            if Player then
                print("Teleport")
                -- Player.Character.HumanoidRootPart.CFrame = target.CFrame + Vector3.new(0, 5, 0)
            end
        end
    end)
end

while true do
    local zeit = timeValue.Value
    zeit = zeit - 1
    timeValue.Value = zeit
    game.Workspace.wall1.SurfaceGui.Frame.TextLabel.Text = zeit
    wait(1)
    print(zeit)
    if zeit == 0 then
        teleport()
        timeValue.Value = 20
        -- 可选:如果需要zeit重置后立即禁用触发,取消注释下面两行
        -- touchConnection:Disconnect()
        -- touchConnection = nil
    end
end

内容的提问来源于stack exchange,提问作者LCyclix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 14:01:30