Roblox商店不扣款
Roblox store not deducting money
我有一家 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)
它没有给我一个错误,它只是按照我解释的去做。如果您需要我更具体一点,那就问吧!我对此很陌生,所以我可能不是最好的。 :) 非常感谢任何帮助!
我认为您的问题可能是您在第一个脚本中更改了客户端而不是服务器上的 leaderstats 值。服务器不知道该更改,并且在对您的硬币值进行其他更新时不会考虑它。
相反,您应该更改您在第一个脚本中调用的 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" 来保存它的价格,则不需要将价格从客户端发送到服务器。
我有一家 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)
它没有给我一个错误,它只是按照我解释的去做。如果您需要我更具体一点,那就问吧!我对此很陌生,所以我可能不是最好的。 :) 非常感谢任何帮助!
我认为您的问题可能是您在第一个脚本中更改了客户端而不是服务器上的 leaderstats 值。服务器不知道该更改,并且在对您的硬币值进行其他更新时不会考虑它。
相反,您应该更改您在第一个脚本中调用的 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" 来保存它的价格,则不需要将价格从客户端发送到服务器。