I am making a shop for my game, which is for my “Rebirths” leaderstat. I am trying to make it so that when the player clicks on the button, it copies the item from serverstorage and plays a purchase sound, and if they don’t have enough, it plays a buzzer sound. I got it near close to working at one of the times, then I decided to use AI to see if I could troubleshoot it, and it got messed up more. Here is what I have so far!
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local remoteEvent = ReplicatedStorage:WaitForChild("PurchaseGravityCoil")
local button = script.Parent
local clickSound = button:WaitForChild("ClickSound")
local boughtSound = button:WaitForChild("Bought")
local notEnoughSound = button:WaitForChild("NotEnough")
button.MouseButton1Click:Connect(function()
clickSound:Play()
remoteEvent:FireServer()
end)
and
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local purchaseGravityCoil = ReplicatedStorage.PurchaseGravityCoil
local gravityCoilTemplate = ServerStorage.GravityCoil
local REBIRTH_COST = 50
purchaseGravityCoil.OnServerEvent:Connect(function(player: Player)
local playerData = player:FindFirstChild("PlayerData")
if not playerData then return end
local rebirths = playerData:FindFirstChild("Rebirths") :: IntValue
if not rebirths then return end
local hasEnoughRebirths = rebirths.Value >= REBIRTH_COST
if hasEnoughRebirths then
rebirths.Value -= REBIRTH_COST
local playerBackpack = player:FindFirstChildWhichIsA("Backpack")
local gravityCoil = gravityCoilTemplate:Clone()
gravityCoil.Parent = playerBackpack
end
end)
If you want to get the result after the event on the server you can use remote function, it would look something like this.
About the callback in the remote event. I don’t know if remote event supports functions as an argument, but it is in any case as insecure as possible, because exploiters can execute ANY function on the server in this way
Local
local button:TextButton = script.Parent
local player = game.Players.LocalPlayer
local clickSound = button:WaitForChild("ClickSound")
local boughtSound = button:WaitForChild("Bought")
local notEnoughSound = button:WaitForChild("NotEnough")
button.MouseButton1Click:Connect(function()
if clickSound then
clickSound:Play()
end
local RemoteFunction = game.ReplicatedStorage:WaitForChild("PurchaseGravityCoil")
local hadEnoughRebiths = RemoteFunction:InvokeServer()
if hadEnoughRebiths then
if boughtSound then
boughtSound:Play()
end
else
if notEnoughSound then
notEnoughSound:Play()
end
end
end)
Sever
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local purchaseGravityCoil:RemoteFunction = ReplicatedStorage.PurchaseGravityCoil
local gravityCoilTemplate = ServerStorage.GravityCoil
local REBIRTH_COST = 50
purchaseGravityCoil.OnServerInvoke:Connect(function(player: Player)
local playerData = player:FindFirstChild("PlayerData")
if not playerData then return end
local rebirths = playerData:FindFirstChild("Rebirths") :: IntValue
if not rebirths then return end
local hasEnoughRebirths = rebirths.Value >= REBIRTH_COST
if hasEnoughRebirths then
rebirths.Value -= REBIRTH_COST
local playerBackpack = player:FindFirstChildWhichIsA("Backpack")
local gravityCoil = gravityCoilTemplate:Clone()
gravityCoil.Parent = playerBackpack
return true
end
return
end)