首页 > 解决方案 > 为什么代码没有运行。它在我的循环中没有做任何事情。| 罗布洛克斯工作室

问题描述

它都不起作用。你能帮忙吗?这是在 Roblox Studio 中。

    local Time = math.floor (game.Lighting:GetMinutesAfterMidnight(480/60))
    local intakeAlert = game.StarterGui.Main.IntakeAlert
    local prisonerCount = game.ReplicatedStorage.NumPrisoners
    local intake = game.ReplicatedStorage.PrisonerIntake -- This is the prisoner intake status
    
    while wait() do
        if Time == 8 then -- Checks if it is 8 hours after midnight.
            print("Prison Bus Arriving.")
            intakeAlert.Visible = true
            wait(2)
            intakeAlert.Visible = false
        end
    end

标签: roblox

解决方案


看完你的代码后,我可以说它没有运行的原因是因为这行代码:

if Time == 8 then -- Checks if it is 8 hours after midnight.

主要原因可能是您的时间变量。

local Time = math.floor (game.Lighting:GetMinutesAfterMidnight(480/60))

首先,GetMinutesAfterMidnight()不接受任何参数。我不知道为什么有一个

其次,您在运行时设置变量,这意味着它的值是静态的。如果运行时的游戏时间为 2,则时间变量将保持为 2,直到脚本停止。

这是我对此的解决方法:

local intakeAlert = game.StarterGui.Main.IntakeAlert
local prisonerCount = game.ReplicatedStorage.NumPrisoners
local intake = game.ReplicatedStorage.PrisonerIntake -- This is the prisoner intake status

local debounce -- Make sure this event doesn't fire several times

while true do
    local currentTime = math.floor(game.Lighting.ClockTime) -- You can change this to GetMinutesBeforeMidnight if you want. Might have to do some math.

    if currentTime == 8 then -- Checks if it is 8 hours after midnight. 
        if debounce then return end -- Return if the event has already been fired.

        debounce = true
        print("Prison Bus Arriving.")
        intakeAlert.Visible = true
        wait(2)
        intakeAlert.Visible = false
    else
        debounce = false -- If its 9 after midnight, debounce will be set to false for the next day.
    end

    wait() -- You should avoid using this, I suggest changing it to something like RunService.Heartbeat:Wait() but that's your choice.
end

推荐阅读