Roblox Studio中设置玩家Money leaderstat为0后数值自动复原问题排查
问题核心原因
你遇到的数值回弹问题确实是循环执行的moneyGain脚本导致的:reseter脚本将Money设为0的时间点,刚好可能卡在moneyGain循环读取旧Money值、还没写回叠加后新值的间隙,最终旧值叠加增益后覆盖了你设置的0,看起来就像数值自动恢复。
解决方案
1. 新增重置状态锁阻断加钱逻辑
首先在对应tycoon的info节点下新增一个BoolValue,命名为IsResetting,默认值设为false。
修改tycoon reseter脚本
调整操作顺序,先加锁、再迁移建筑按钮、最后修改金钱,确保迁移完成后不会再触发旧按钮的加钱调用,延迟足够时间后再解锁:
local player = script.Parent.Parent.Parent.Parent.Parent.Parent script.Parent.Parent.Parent.Visible = false script.Parent.MouseButton1Click:Connect(function() -- 先加重置锁,阻断所有加钱逻辑 local tycoonInfo = player.claimedTycoon.Value.info tycoonInfo.IsResetting.Value = true script.Parent.Parent.Parent.Visible = false local savedItems = tycoonInfo.savedItems.Value local builds = player.claimedTycoon.Value.activeBuilds:GetChildren() local buttons = player.claimedTycoon.Value.activeButtons:GetChildren() -- 先迁移所有建筑和按钮,避免循环读取到旧按钮触发加钱 for i = 1, #builds, 1 do builds[i].Parent = savedItems.builds end for j = 1, #buttons, 1 do buttons[j].Parent = savedItems.buttons end savedItems.builds.dirtStarterBase.Parent = player.claimedTycoon.Value tycoonInfo:WaitForChild("activeWave").Value = 1 -- 所有重置逻辑完成后再修改金钱值 player.leaderstats.Money.Value = 0 -- 等待超过moneyGain的循环间隔(0.24秒)后再解锁,确保上一轮循环完全执行完成 task.wait(0.3) tycoonInfo.IsResetting.Value = false end)
修改moneyEnforcer脚本
在OnInvoke回调最开头加重置状态判断,重置期间直接跳过加钱逻辑:
local rs = game:GetService("ReplicatedStorage") local dictionary = require(rs.dictionary) local vipPlayers = dictionary.vipPlayers -- 新增MarketplaceService调用,替换废弃的GamePassService local MarketplaceService = game:GetService("MarketplaceService") script.enforce.OnInvoke = function(amount) -- 重置状态直接返回,不执行加钱 if script.Parent.Parent.info.IsResetting.Value then return end print(amount) if script.Parent.Parent.info.player.Value ~= nil then local player = script.Parent.Parent.info.player.Value -- 替换废弃接口 local hasVipPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, 25494219) if hasVipPass then amount = amount * 2 else for i = 1, #vipPlayers, 1 do if player.Name == vipPlayers[i] then amount = amount * 2 end end end player.leaderstats.Money.Value += amount end end
修改moneyGain脚本
给OWNERDOOR的加钱逻辑也加重置状态判断:
local playerMoney while task.wait(0.24) do local tycoonInfo = script.Parent.Parent.info local buttons = tycoonInfo.savedItems.Value.buttons:GetChildren() for button = 1, #buttons, 1 do if buttons[button].Parent.Parent.tycoon.Value ~= nil then if buttons[button].Parent.Parent.tycoon.Value.info.owner ~= nil then if buttons[button].jobDone.Value == true then script.Parent.moneyEnforcer.enforce:Invoke(buttons[button].gainVal.Value) end end end end --OWNERDOOR: 增加重置状态判断 if tycoonInfo.owner.Value ~= nil and not tycoonInfo.IsResetting.Value then script.Parent.moneyEnforcer.enforce:Invoke(1) end end
2. 额外优化点
上述修改中已经将废弃的GamePassService:PlayerHasPass替换为官方推荐的MarketplaceService:UserOwnsGamePassAsync,避免接口过期导致的潜在数值异常;同时将旧的wait()方法替换为性能更稳定的task.wait()。
内容的提问来源于stack exchange,提问作者rabitailleow
相关产品推荐
相关产品推荐

