【问题标题】:How can i make it so a change of a IntValue is permanent?我怎样才能让它改变 IntValue 是永久性的?
【发布时间】:2021-05-02 10:29:04
【问题描述】:

所以我尝试制作一个游戏通行证(通过 Robux 赚钱的开发人员产品)gui,并且在对其进行了几次测试时,它确实有效,直到我达到了大约 400,000-500,000 根香蕉(这是我的货币)的某个价值。停止工作。另外,在制作游戏的早期,当我购买价值超过 100 万根香蕉的东西时,它似乎会减少,但是当我得到一些香蕉种子(可出售物品)并出售它时,它会重置回我购买 100 万根之前香蕉项目(例如,我从 130 万购买它,然后它变成 300,000,但是当价值被修改时(例如:通过出售种子)它会变回购买之前(130 万))并且我也在做管理员GUI,我想制作一个 gui 来赚钱,显然我一开始就知道它不会起作用。

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

game.Players.PlayerAdded:Connect(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
      --[Donate to me!]
        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:Connect(function()
     if 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 的菜鸟,如果这个问题不够详细,我很抱歉,如果你愿意,我会提供更多详细信息。

【问题讨论】:

  • 请检查发布的代码。你缺少引号。仅在此处还是在您的实际脚本中?那么你应该面临错误
  • 什么意思?
  • 我的代码工作正常,只是它没有保存它的价值,并重置到我购买该工具之前
  • 你错过了"来关闭"Rebirths
  • 如果你在谈论代码块之外的一些代码,那是我在stackoverflow中犯的一个错误,我放了四个空格,但它一直让它看起来像是在代码之外跨度>

标签: lua roblox


【解决方案1】:

您的问题的简短回答是:在服务器上完成工作。

The server is the source of truth for all values。当服务器Script 更改世界时,该更改将复制到所有玩家。但是当使用LocalScript 更改世界时,该更改只会出现在该特定玩家的世界中。

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

所以在 ReplicatedStorage 中创建一个 RemoteEvent 并将其命名为 BuyItem

在您的 LocalScript 中:

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

local Button = script.Parent
Button.MouseButton1Up:Connect(function()
     -- tell the server that the player would like to buy the item
     BuyItem:FireServer("SmoothPlasticWand")
end)

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

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

-- set up a price table for items
local prices = {
    SmoothPlasticWand = 1000000,

    -- add more items here
    -- SomeOtherWand = 10,
}

-- listen for the player wanting to buy things
BuyItem.OnServerEvent:Connect(function(player, itemName)
    -- check that it's a real item
    assert(type(itemName) == "string", "Expected itemName to be a string")
    assert(prices[itemName], "Could not find a price for " .. itemName)

    -- check that the player has enough money
    local itemPrice = prices[itemName]
    local currency = player.leaderstats.Bananas
    if currency.Value >= itemPrice then
        -- give the player their wand
        local wand = ReplicatedStorage.ReplicatedWands[itemName]:Clone()
        wand.Parent = player.Backpack

        -- subtract how much the wand cost
        currency.Value = currency.Value - itemPrice
    end
end)

现在,由于服务器脚本正在处理对 currency.Value 的更改,因此该更改将正确显示给所有人并使更改永久化。

【讨论】:

    【解决方案2】:

    也许为了安全起见,请执行以下操作将其重置为应有的状态。基本上制作一个变量来捕获货币的价值,然后使货币与故障保险具有相同的价值

    --在购买物品后立即放置 StoredBananas.Value = coin.Value

    --这是在购买时货币变化之后发生的,以便在出现故障后将其设置为正确的价格。

    coins.Value = StoredBananas.Value

    【讨论】:

    • StordBananas.Value = coin.Value
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 2014-08-11
    • 1970-01-01
    • 2022-10-01
    • 1970-01-01
    • 2022-01-24
    相关资源
    最近更新 更多