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

求助:GnomeCode塔防脚本出现Infinite yield错误,塔无法显示

塔防游戏塔显示空白+无限Yield错误求助

我正在跟着GnomeCodes的塔防教程视频制作塔防游戏,目前完成到最后一节**"Getting Players Chosen Towers"**环节,但测试时塔空白无法显示,同时收到以下错误提示:

18:13:51.541  Infinite yield possible on 'ReplicatedStorage.Events:WaitForChild("GetData")'  -  Studio
  18:13:51.541  Stack Begin  -  Studio
  18:13:51.541  Script 'ServerScriptService.Datastore', Line 9  -  Studio - Datastore:9
  18:13:51.541  Stack End  -  Studio

我的服务器脚本如下:

local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")

local database = DataStoreService:GetDataStore("database")
local functions = ReplicatedStorage:WaitForChild("Events")
local events = ReplicatedStorage:WaitForChild("Functions")
local getDataFunc = functions:WaitForChild("GetData")
local exitEvent = events:WaitForChild("ExitGame")

local MAX_SELECTED_TOWERS = 5

local data = {}

local function LoadData(player)
    local success = nil
    local playerData = nil
    local attempt = 1

    repeat
        success, playerData = pcall(function()
            return database:GetAsync(player.UserId)
        end)
    
        attempt += 1
        if not success then
            warn(playerData)
            task.wait()
        end
    
    until success or attempt == 3

    if success then
        print("Connection Success")
        if not playerData then
            print("New Player, Giving Default Data")
            playerData = {
                ["Credits"] = 175,
                ["SelectedTowers"] = {"Iron Man MK5"},
                ["OwnedTowers"] = {"Iron Man MK5", "Wolverine"}
            }
        end
        data[player.UserId] = playerData
    else
        warn("Unable to get data for player", player.UserId)
        player:Kick("There was a problem getting your data, We're Sorry!")
    end
end
Players.PlayerAdded:Connect(LoadData)

local function SaveData(player)
    if data[player.UserId] then
        local success = nil
        local playerData = nil
        local attempt = 1

        repeat
            success, playerData = pcall(function()
                return database:UpdateAsync(player.UserId, function()
                    return data[player.UserId]
                end)
            end)

            attempt += 1
            if not success then
                warn(playerData)
                task.wait()
            end

        until success or attempt == 3

        if success then
            print("Data Saved Successfully")
        else
            warn("Unable to get save data for", player.UserId)
        end
    else
        warn("No Session Data For", player.UserId)
    end

end
exitEvent.OnServerEvent:Connect(function(player)
    SaveData(player)
    data[player.UserId] = nil
end)

game:BindToClose(function()
    if not RunService:IsStudio() then
        for index, player in pairs(Players:GetPlayers()) do
                task.spawn(function()
                SaveData(player)
            end)
        end
    else
        print ("Shutting Down Inside Studio")
    end
end)


getDataFunc.OnServerInvoke = function(player)
    return data[player.UserId]
end

希望有人能帮忙解决这个问题,非常感谢。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 02:17:02