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

Roblox Studio函数意外重复执行问题求助

Roblox脚本重叠执行问题修复

你的问题核心在于每次eids.Value变化时,都会启动一个新的无限while循环,但之前的循环并没有被终止,导致多个循环同时运行、互相干扰,出现重叠执行的情况。

问题根源

你的eids.Changed事件连接中,每次触发都会执行内部代码,包括启动那个while wait()的无限循环。旧的循环会一直在后台运行,新的循环又会被启动,最终多个循环同时执行,引发异常。

解决方案

我们需要在启动新循环前,先终止上一次的循环。可以用变量保存当前运行的循环协程,每次触发Changed时先关闭旧协程,再开启新的。

修改后的完整代码:

local eids = script.Parent.EnemyIDSelected
local userinput = game:GetService("UserInputService")
local frame = script.Parent.Parent.Parent
local enemycount = frame.Enemies.NumberOfEnemies
local enemyorder = frame.Enemies.Frame:WaitForChild("EnemyOrder")

-- 保存当前运行的循环协程
local currentCoroutine = nil

userinput.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.Left then
        eids.Value = eids.Value - 1
    elseif input.KeyCode == Enum.KeyCode.D or input.KeyCode == Enum.KeyCode.Right then
        eids.Value = eids.Value + 1
    end
end)

eids.Changed:Connect(function()
    if eids.Value > enemycount.Value then
        eids.Value = 1
    elseif eids.Value < 1 then
        eids.Value = enemycount.Value
    end
end)

eids.Changed:Connect(function()
    -- 终止之前运行的协程
    if currentCoroutine and coroutine.status(currentCoroutine) == "running" then
        coroutine.close(currentCoroutine)
    end

    local number = 0

    local function findenemies()
        local enemycount = frame.Enemies.NumberOfEnemies.Value
        local enemies = {}
        local ordernumber = 1
        for i, v in pairs(enemyorder:GetChildren()) do
            if string.sub(v.Name, 1, 5) == "Order" then
                enemies[ordernumber] = v.Value
                if ordernumber == enemycount then
                    return enemies
                end
                ordernumber = ordernumber + 1
            end
        end
    end
    local enemies = findenemies()

    -- 将循环包装为协程并启动
    currentCoroutine = coroutine.create(function()
        while true do
            for i, v in pairs(enemies) do
                if i == eids.Value then
                    print(number)
                    script.Parent.ImageLabel.TextLabel.Text = enemies[i].EnemyValue.Value
                    
                    -- 统一设置所有敌人为灰色
                    for _, v2 in pairs(enemies) do
                        v2.ImageColor3 = Color3.fromRGB(80, 80, 80)
                    end

                    -- 闪烁当前选中的敌人
                    task.wait(0.15)
                    enemies[i].ImageColor3 = Color3.fromRGB(255, 255, 255)
                    task.wait(0.15)
                    number = number + 1
                end
            end
            task.wait()
        end
    end)
    coroutine.resume(currentCoroutine)
end)

额外优化说明

  • 使用task.wait()替代wait(),这是Roblox官方推荐的更高效的等待函数。
  • 简化了敌人颜色设置逻辑:先统一将所有敌人设为灰色,再单独处理当前选中敌人的闪烁,代码更清晰。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 18:01:12