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

如何通过TextButton提升玩家属性?我的实现代码失效求解决

问题分析与修复方案

核心问题点

  • 死循环阻塞事件绑定:while true循环位于按钮事件绑定代码之前,脚本执行到此处会持续占用主线程,导致后续的按钮点击事件永远无法被注册,按钮自然不会响应操作。
  • 属性值未实时获取:初始化时定义的df和s仅保存了脚本启动时的属性初始值,点击按钮时不会读取当前最新的属性数据,导致属性更新始终基于初始值计算,而非当前实际值。
  • 点数未消耗:满足条件点击按钮后,未扣除对应的Exp点数,逻辑上允许玩家无限点击而不消耗资源。

修复后的代码

local localplr = game.Players.LocalPlayer
local char = localplr.Character or localplr.CharacterAdded:Wait()

local dfb = script.Parent.Frame.DevilFruitButton
local sb = script.Parent.Frame.StrenghtButton

-- 将UI更新逻辑放入独立线程,避免阻塞主线程
task.spawn(function()
    while true do
        task.wait(5)
        -- 实时获取最新属性值用于UI更新
        local currentExp = localplr:GetAttribute("Exp")
        local currentStrength = localplr:GetAttribute("Strenght")
        local currentDevilFruit = localplr:GetAttribute("DevilFruit")
        
        script.Parent.Frame.Points.Text = "可用点数 - " .. currentExp
        script.Parent.Frame.Strenght.Text = "力量 - " .. currentStrength
        script.Parent.Frame.DevilFruitPoints.Text = "恶魔果实 - " .. currentDevilFruit
    end
end)

dfb.MouseButton1Down:Connect(function()
    local currentExp = localplr:GetAttribute("Exp")
    if currentExp >= 1 then
        -- 实时获取当前恶魔果实属性并更新
        local currentDF = localplr:GetAttribute("DevilFruit")
        localplr:SetAttribute("DevilFruit", currentDF + 1)
        -- 扣除1点可用点数
        localplr:SetAttribute("Exp", currentExp - 1)
    end
end)

sb.MouseButton1Down:Connect(function()
    local currentExp = localplr:GetAttribute("Exp")
    if currentExp >= 1 then
        -- 实时获取当前力量属性并更新
        local currentStrength = localplr:GetAttribute("Strenght")
        localplr:SetAttribute("Strenght", currentStrength + 1)
        -- 扣除1点可用点数
        localplr:SetAttribute("Exp", currentExp - 1)
    end
end)

额外提示

代码中Strenght为拼写错误,正确拼写应为Strength。如果服务器端的属性名使用的是正确拼写,请同步修改所有相关的属性引用,避免出现属性无法读取/更新的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 13:57:20