IntValue 始终等于 0,即使它不是

问题描述 投票:0回答:2

所以我遇到了一个问题,我试图获取 IntValue 的值,但它总是给我 0,即使该值不为 0。没有出现错误,它位于服务器脚本内。我不知道问题出在哪里:

local leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"

local Clicks = Instance.new("IntValue", leaderstats)
Clicks.Name = "Clicks"

local Multiplier = Instance.new("IntValue", leaderstats)
Multiplier.Name = "Multiplier"

-- a bunch of datastore stuff


game.Players.PlayerRemoving:Connect(function(plr)
print("removing")
local approved, errortext = pcall(function()
    print(plr.leaderstats.Clicks.Value, plr.leaderstats.Multiplier.Value)
    datastore:SetAsync(plr.UserId.."-save", {plr.leaderstats.Clicks.Value, plr.leaderstats.Multiplier.Value})
    
end)

if approved then
    
    print("data saved")
    print( plr.leaderstats.Multiplier.Value, plr.leaderstats.Clicks.Value)
else
    
    print("idiot data wont save")
    
end

end)

它每次都只打印 0 作为乘数值。

这是更改乘数值的代码:

local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local clicks = leaderstats.Clicks
local folder = script.Parent.multipliers
local multiplier = leaderstats.Multiplier

for i,v in pairs(folder:GetChildren()) do

local cost = v.Cost

v.MouseButton1Click:Connect(function()
    
    print(v)
    
    if multiplier.Value > tonumber(v.Name) then
        
        print("already bbogt")
        
    end
    
    if multiplier.Value + 1 == tonumber(v.Name) then
        
        if cost.Value > clicks.Value then
            
            print("not enough moneys smh my head")
            
        elseif cost.Value <= clicks.Value then
            
            print("rich smh mynhead")
            
            clicks.Value -= cost.Value
            
            multiplier.Value = multiplier.Value + 1
        end
        
        
    elseif multiplier.Value == 0 and v.Name == "2" then
        if 5 > clicks.Value then

            print("not enough moneys smh my head")

        elseif 5 <= clicks.Value then

            print("rich smh mynhead")

            clicks.Value -= 5

            multiplier.Value = 2
            print(clicks.Value)
        end
        
    end
    
end)

end
lua roblox
2个回答
0
投票

您如何更改乘数的值?当您从脚本更改乘数的值时,请从资源管理器手动检查该值是否不为 0。例如,在此代码中,它确实更改了变量的值,但没有更改实际 Intvalue 的值。

local Player = game.Players.LocalPlayer
local Intvalue = Player.leaderstats.IntValue.value

IntValue = IntValue + 10

基本上,这里发生的是游戏获取 IntValue 的值并将其作为变量返回。那么当你改变变量的值时,它确实会增加变量的值,但不会改变资源管理器中intvalue的实际值。所以这样做会更好:

local Player = game.Players.LocalPlayer
local IntValue = Players.leaderstats.IntValue

IntValue.value = IntValue.value + 10

基本上,这会更改资源管理器中实际 Intvalue 的值,而不是其他仅更改变量的示例。因此,如果您在第一个示例中打印变量“IntValue”,它仍然会像第二个示例一样打印 10,但这会打印变量的值而不是实际值。

我认为您可能以错误的方式更改了它的值,因此它实际上并没有改变它,所以它在输出中显示“0”。但如果资源管理器中的值实际上不是 0,那么我真的不知道我是否能提供多大帮助。

而且我不是一个很好的解释者,所以如果我没有很好地解释我的观点,那么基本上只需在资源管理器中手动检查乘数的值即可。并确保您正在更改乘数的实际值,而不仅仅是脚本中某处的变量。


0
投票

代码内部,value变量是不断更新还是已经设置了值?如果该值在其他行之前设置为变量一次,则一定意味着代码尚未更新该值,使其保持为 0。如果这两个代码批次循环,那么我不太确定,但只需仔细检查代码是否是递归循环,因此值可以更新。您可以简单地添加一个包含 wait() 的 while 循环,以便代码递归运行。

(下面的这些变量必须通过 while 循环进行更新)

local clicks = leaderstats.Clicks
local folder = script.Parent.multipliers
local multiplier = leaderstats.Multiplier

或者如果你想要变量在前面,那么整个脚本必须嵌套在一个 while 循环中,就像这样......

while true do
wait()
local leaderstats = game.Players.LocalPlayer:WaitForChild("leaderstats")
local clicks = leaderstats.Clicks
local folder = script.Parent.multipliers
local multiplier = leaderstats.Multiplier

for i,v in pairs(folder:GetChildren()) do

local cost = v.Cost

v.MouseButton1Click:Connect(function()
    
    print(v)
    
    if multiplier.Value > tonumber(v.Name) then
        
        print("already bbogt")
        
    end
    
    if multiplier.Value + 1 == tonumber(v.Name) then
        
        if cost.Value > clicks.Value then
            
            print("not enough moneys smh my head")
            
        elseif cost.Value <= clicks.Value then
            
            print("rich smh mynhead")
            
            clicks.Value -= cost.Value
            
            multiplier.Value = multiplier.Value + 1
        end
        
        
    elseif multiplier.Value == 0 and v.Name == "2" then
        if 5 > clicks.Value then

            print("not enough moneys smh my head")

        elseif 5 <= clicks.Value then

            print("rich smh mynhead")

            clicks.Value -= 5

            multiplier.Value = 2
            print(clicks.Value)
        end
        
    end
    
end)
end
end

如果这不能让您满意,请尝试对这个问题进行更多研究。

© www.soinside.com 2019 - 2024. All rights reserved.