Roblox商店未扣除钱款

我有一个Roblox商店,每当我购买东西时,它会从我的leaderstats中扣除金额,但是当我去获取更多的钱时,它会将我刚获得的钱与我刚刚花费的钱一起添加。这是我拥有的代码

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("Coins").Value >= price.Value then
        player.leaderstats:FindFirstChild("Coins").Value = player.leaderstats:FindFirstChild("Coins").Value - price.Value
        game.ReplicatedStorage.ShopBuy:FireServer(tool.Value)
    end
end)

这是其中一个赚钱的脚本。

db = false
script.Parent.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        if db == false then
            db = true
            script.Parent.BrickColor = BrickColor.new("Bright red")
            player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + 100
            wait(10)
            script.Parent.BrickColor = BrickColor.new("Dark green")
            db = false

        end
    end
end)

它没有给我任何错误,它只是做了我解释的事情。如果你需要我更具体地描述一下,就问我吧!我对此很陌生,所以我可能不是最好的。:)非常感谢您的帮助!

点赞
用户1296374
用户1296374

我认为你的问题可能是在第一个脚本中将 leaderstats 值改为了客户端而不是服务器。那么服务器不知道这个改变,并且在更新 Coins 值时也不会考虑它。

相反,你应该在你的第一个脚本中调用 ShopBuy 事件处理程序来改变 Coins 值。

更新:

我认为你的解决方案可能是来自 YouTube 上的教程步骤……我没有经历过那些,但我会尝试以下内容:

在购买按钮下面的客户端脚本中,去掉更改金额的那一行,而是将价格值发送到服务器:

game.ReplicatedStorage.ShopBuy:FireServer(tool.Value,price.Value)

然后在服务器的 ShopBuy 脚本中,改变回调签名以包括新参数,像这样:

game.ReplicatedStorage.ShopBuy.OnServerEvent:Connect(function(player,tool,price)

然后在该函数中添加减去金钱的方法:

player.leaderstats.Money.Value = layer.leaderstats.Money.Value - price

想想吧,现在服务器正在执行扣款命令,因此所有客户端都会知道它。

还有一个备注:如果你在每个工具中创建一个“IntValue”,其中包含其价格,你就不需要将价格从客户端发送到服务器。

2020-04-24 04:40:35