I’m trying to make a shop where if the player has enough coins, they are able to buy a gun and a certain amount of money is taken from their balance, which then goes into their backpack.
this is a script inside the button.
local RS = game:GetService("ReplicatedStorage")
local BuyPistol = RS:WaitForChild("BuyPistol")
script.Parent.MouseButton1Down:Connect(function()
BuyPistol:FireServer()
end)
this is a script inside the remote event:
local GunPrice = 500
game.Players.PlayerAdded:Connect(function(player)
script.Parent.OnServerEvent:Connect(function()
if player.leaderstats.Cash.Value >= GunPrice then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - GunPrice
wait(1)
script.Parent.Parent.Luger.Parent = player.Backpack
end
end)
end)
my problem is, there is no money taken from me and i dont get the item.
As @Sanches8002 said, your script won’t execute in ReplicatedStorage. I suggest putting it in ServerScriptService or the shop GUI.
Although not part of the original question, I’d also like to make a few suggestions to your scripts.
Using a PlayerAdded event to get the player is redundant here, FireServer parses the player object to the OnServerEvent.
Since this is a shop and you’re likely to have multiple items, instead of having a fixed gun price, you can parse the price to the function.
Here you are parenting the item to a player which means in any server, the first player to buy the item will be the only one to receive it. Rather store the item in ServerStorage, clone it then parent it to the players backpack.
So your final scripts should look something like this:
local BuyItem = game.ReplicatedStorage:WaitForChild("BuyItem")
script.Parent.MouseButton1Down:Connect(function()
BuyItem:FireServer("Luger", 500)
end)
and
local BuyItem= game.ReplicatedStorage:WaitForChild("BuyItem")
BuyItem.OnServerEvent:Connect(function(player, Name, GunPrice)
if player.leaderstats.Cash.Value >= GunPrice then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - GunPrice
wait(1)
local itemClone = game.ServerStorage[Name]:Clone()
itemClone.Parent = player.Backpack
end
end)
I see a few problems, but I don’t know if they are your main problem. Mainly, you are using RemoteEvents incorreclty. You don’t need to wrap the RemoteEvent.OnServerEvent inside of Players.PlayerAdded. The RemoteEvent sends the player when the event is fired. You can change your server script to look like this:
local GunPrice = 500
script.Parent.OnServerEvent:Connect(function(player)
if player.leaderstats.Cash.Value >= GunPrice then
player.leaderstats.Cash.Value = player.leaderstats.Cash.Value - GunPrice
wait(1)
script.Parent.Parent.Luger.Parent = player.Backpack
end
end)
Also, what @Sanches8002 said is correct, scripts aren’t executed in ReplicatedStorage. It would be better to place the script into ServerScriptService and create a variable to reference to the event. Like this:
local event = game:GetService("ReplicatedStorage"):WaitForChild([EventName])
If you have any other questions, feel free to ask them. If this doesn’t work for you, feel free to respond and I will gladly help out.