如何使IntValue的更改是永久的?

我正在制作一个游戏通行证(通过Robux获取金钱的开发者服务),并在测试几次后,它有效,直到我达到约400,000-500,000香蕉(我的货币)的某个值时停止工作。而且,在制作游戏时,当我购买某些成本超过100万香蕉的物品时,它似乎会减少,但是当我获得一些香蕉种子(可出售的物品)并出售它时,它会重新设置回购买100万香蕉物品之前的值(例如:从130万然后我购买它,然后它变成30万,但当值被修改时(例如:通过出售种子),它会改回购买之前的值(130万)),我还在制作一个管理GUI,并想要制作一个GUI以给钱,显然我知道一开始它不会工作。

Leaderstats脚本: 脚本名称:leaderstats 脚本类型:服务器端 脚本代码:

game.Players.PlayerAdded:连接(function()
     local leaderstats = Instance.new("Folder")
     leaderstats.Name = "leaderstats"

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

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

     local bananaseeds = Instance.new("IntValue", leaderstats)
     bananaseeds.Name = "BananaSeeds"
end

我的游戏通行证处理程序脚本(ServerScriptService): 类型:服务器端 名称:DevPrdctHndlr 代码:

local mps = game:GetService("MarketplaceService")

mps.ProcessReceipt = function(reciptInfo)
 if reciptInfo.ProductId = 1146099164 then
      --[捐赠给我!]
        local player = game.Players:GetPlayerByUserId(reciptInfo.PlayerId)
        player.leaderstats.BananaSeeds.Value =
        player.leaderstats.BananaSeeds.Value + 10000
        player.leaderstats.Bananas.Value =
        player.leaderstats.Bananas.Value + 50000
        return Enum.ProductPurchaseDecision.PurchaseGranted
    end
end

购买SmoothPlasticWand脚本(它是一个购物GUI文本按钮): 类型:本地脚本 名称:BuyScript 位置:game.StarterGui.ShopGui.ShopFrame.SmoothPlasticWand.BuyScript 代码:

local Button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local currency =
game.Players.LocalPlayer.leaderstats:WaitForChild("Bananas")

Button.MouseButton1Up:连接(function()
     如果货币价值(currency.Value)大于等于1000000 then


 ReplicatedStorage.ReplicatedWands.SmoothPlasticWand:Clone().Parent = game.Players.LocalPlayer:WaitForChild("Backpack")
    game.Workspace.Musounds.Buy:Play
    currency.Value = currency.Value - 1000000
end

如果这个问题是重复的,我很抱歉,我还是StackOverflow的新手,如果这个问题不够详细,我会提供更多信息,如果你想要。

点赞
用户2860267
用户2860267

将下面翻译成中文并且保留原本的 markdown 格式

回答你的问题的简短回答是:在服务器上进行操作。

服务器是所有值的真相来源。当服务器脚本改变了世界时,该改变会被复制到所有玩家中。但当LocalScript更改世界时,该更改仅出现在特定玩家的世界中。

因此,在LocalScript中使用MouseButton1Up处理程序对按钮进行更改时,您只会更改玩家对该值的本地视图,而不会更改真相来源。其中一个解决方法是在LocalScript中使用RemoteEvent向服务器发出需要进行更改的信号。

因此在ReplicatedStorage中创建一个名为“BuyItem”的RemoteEvent

在您的LocalScript中:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuyItem = ReplicatedStorage.BuyItem

local Button = script.Parent
Button.MouseButton1Up:Connect(function()
     -- 告诉服务器玩家想要购买该物品
     BuyItem:FireServer("SmoothPlasticWand")
end)

然后,在某个地方的脚本中,处理客户端发来的信号:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local BuyItem = ReplicatedStorage.BuyItem

-- 为物品设置价格表
local prices = {
    SmoothPlasticWand = 1000000,

    -- 在此添加更多物品
    -- SomeOtherWand = 10,
}

-- 监听玩家购买物品
BuyItem.OnServerEvent:Connect(function(player, itemName)
    -- 检查它是否为真实物品
    assert(type(itemName) == "string", "Expected itemName to be a string")
    assert(prices[itemName], "Could not find a price for " .. itemName)

    -- 检查玩家是否有足够的钱
    local itemPrice = prices[itemName]
    local currency = player.leaderstats.Bananas
    if currency.Value >= itemPrice then
        -- 给玩家他们的魔杖
        local wand = ReplicatedStorage.ReplicatedWands[itemName]:Clone()
        wand.Parent = player.Backpack

        -- 减去魔杖的价格
        currency.Value = currency.Value - itemPrice
    end
end)

现在,因为服务器脚本正在处理对“currency.Value”的更改,所以该更改将正确地显示给每个人,使更改变得永久。

2021-01-30 09:45:40
用户18032736
用户18032736

也许为了安全起见,可以像这样重置它到它应该的值。基本上创建一个变量来捕捉货币的价值,然后将货币设置为相同的值作为故障安全保护。

--在购买物品后立即执行此操作 StoredBananas.Value = coins.Value

--当购买货币变化时,执行此操作,以便在它发生故障时将其设置为正确的价格。

coins.Value = StoredBananas.Value

2022-01-25 23:08:29