Roblox Studio Lua 问题 "Workspace.Ultimate.Script:12: 尝试比较数字 < 字符串"-

deb = true
giv = script.Parent.Giver
function touch(part)
    local hum = part.Parent:FindFirstChild("Humanoid")
    if hum then
        local plr = game.Players:FindFirstChild(part.Parent.Name)
        local ls = plr:FindFirstChild("leaderstats")
        local cash = ls:FindFirstChild("Cash")
        if plr then
            if deb == true then
                if cash.Value > 12500 then
                deb = false
                giv.BrickColor = BrickColor.new("Really red")
                    local weapon = game.ReplicatedStorage.Jutsus:FindFirstChild("Fireball")
                local w2 = weapon:Clone()
                w2.Parent = plr.Backpack
                wait(script.Parent.RegenTime.Value)
                giv.BrickColor = BrickColor.new("Bright violet")
                    deb = true
                end
            end
        end
    end
end
script.Parent.Giver.Touched:connect(touch)

我卡在这个地方,尝试了很多方法但都不起作用,输出结果是这样的:

Workspace.Ultimate.Script:12: 尝试比较数字 < 字符串  -  服务器 - 脚本:12

如果有人能改进这段代码,我将不胜感激。

点赞
用户2858170
用户2858170

显然 cash.Value 是一个字符串,因此你不能执行 cash.Value > 12500

在将其与数字进行比较之前,将 cash.Value 转换为数字。

tonumber(cash.Value) > 12500

这个解决方案假设 cash.Value 是表示数字的字符串。如果不是这种情况,你需要先确保它是这样的。

2021-05-27 04:04:04
用户15878986
用户15878986

字符串和数字比较需要事先转换,并且你需要确保 cash.Value 必须是数字,否则你可能会提示与空值比较。

然后你可以编写如下代码。

deb = true
giv = script.Parent.Giver
function touch(part)
    local hum = part.Parent:FindFirstChild("Humanoid")
    if hum then
        local plr = game.Players:FindFirstChild(part.Parent.Name)
        local ls = plr:FindFirstChild("leaderstats")
        local cash = ls:FindFirstChild("Cash")
        if plr then
            if deb == true then
                if (tonumber(cash.Value) or 0) > 12500 then
                    deb = false
                    giv.BrickColor = BrickColor.new("Really red")
                    local weapon = game.ReplicatedStorage.Jutsus:FindFirstChild("Fireball")
                    local w2 = weapon:Clone()
                    w2.Parent = plr.Backpack
                    wait(script.Parent.RegenTime.Value)
                    giv.BrickColor = BrickColor.new("Bright violet")
                    deb = true
                end
            end
        end
    end
end
script.Parent.Giver.Touched:connect(touch)
2021-05-28 03:33:57