【问题标题】:Roblox Studio: Shop SystemRoblox Studio:商店系统
【发布时间】:2020-10-25 00:31:03
【问题描述】:

您好,

我有一个可以正常工作的商店系统。它有一个问题。当你从商店买东西时,你的钱就会像想象的那样下降。但是如果你得到更多的钱,你的钱的数量就会恢复到原来的水平,再加上它会给你带来你得到的新钱。我不知道如何解决它。这是代码。

local price = script.Parent.Parent.Price
local tools = game.ReplicatedStorage:WaitForChild("Tools")
local tool = script.Parent.Parent.ItemName
local player = script.Parent.Parent.Parent.Parent.Parent.Parent

script.Parent.MouseButton1Click:connect(function()
    if player.leaderstats:FindFirstChild("Money").Value >= price.Value then 
        player.leaderstats:FindFirstChild("Money").Value = player.leaderstats:FindFirstChild("Money").Value - price.Value
        game.ReplicatedStorage.ShopBuy:FireServer(tool.Value)
    end
end)

这是将物品放入库存的代码:

local tools = game.ReplicatedStorage:WaitForChild("Tools")

game.ReplicatedStorage.ShopBuy.OnServerEvent:Connect(function(player,tool)
    
    local clone = tools:FindFirstChild(tool):Clone()
    clone.Parent = player.Backpack

    local clone2 = tools:FindFirstChild(tool):Clone()
    clone2.Parent = player.StarterGear
end)

【问题讨论】:

  • 这是本地脚本还是服务器脚本?
  • 这是一个本地脚本。

标签: roblox


【解决方案1】:

在 LocalScripts 中所做的更改不会复制到每个人。这意味着更改只会为您显示。如果您想更新您的leaderstats,请在脚本中进行更改。

要解决您的问题,请将价格检查移至处理 ShopBuy RemoteEvent 的脚本中。

local item = script.Parent.Parent
local shopBuy = game.ReplicatedStorage.ShopBuy
script.Parent.MouseButton1Click:connect(function()
    shopBuy:FireServer(item)
end)

然后在你的服务器脚本中解析细节。

local tools = game.ReplicatedStorage.Tools
local shopBuy = game.ReplicatedStorage.ShopBuy

shopBuy.OnServerEvent:Connect(function(player, item)
    -- get some details about the tool
    local toolName = item.ItemName.Value
    local price = item.Price.Value
    local money = player.leaderstats:FindFirstChild("Money")

    -- check that the player has enough money
    if money.Value >= price then
        money.Value = money.Value - price

        -- give the tool
        local clone = tools:FindFirstChild(toolName):Clone()
        clone.Parent = player.Backpack

        local clone2 = tools:FindFirstChild(toolName):Clone()
        clone2.Parent = player.StarterGear
    end
end)

【讨论】:

  • 我不是最好的程序员,所以我觉得我做的不对。
  • 我使用将物品放入库存的脚本编辑了问题。
  • 当你说“它没有工作”时,它没有正确地减去钱吗?钱的问题又发生了吗?它没有给工具吗?有没有报错?
猜你喜欢
  • 2020-03-28
  • 2020-08-05
  • 2018-12-17
  • 2020-12-02
  • 2020-11-27
  • 1970-01-01
  • 2020-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多