Creating A Rebirth Shop!

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!

image

I have a localscript under the button and a script in serverscriptstorage to find the cost and actually take away the amount of Rebirths they use.

1 Like

I don’t think you need a waitforchild() for a remote event, try setting it to just the remote event, without waiting for it.

1 Like

no difference, except sometimes it would error. waitforchild just waits until it exists or times out and warns. ensuring it does exist

I know what it does, Each time I used WaitForChild for remote events it, wouldn’t find it and give an error.

1 Like

You can’t pass functions through RemoteEvents. That’s why your code is not working.

Try this:

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)
2 Likes

I tried this and nothing changed, I also tried taking out the waitforchild() and it had 0 result

1 Like

Follow up when I click on the button it puts into my output PlayerData Folder not found for player

line 23

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)
2 Likes

I’m not sure if this code can be just copied and pasted, I just gave an example of how it would look like using remote function

1 Like

As @Free_Br1cks said, you can’t pass functions through RemoteEvents. In this situation, I would use RemoteFunctions.

Using remote functions, you could return true/false and have it look something like this:

ServerScript:

return hadEnoughRebirths -- Use this instead of "if callback then..."

LocalScript:

if remoteEvent:InvokeServer() then
    -- Play sound
end
1 Like

I got it to work with the RemoteEvent
Server


Local

You need a GravityCoil in ServerStorage (Make sure all names fit with the script if you are using this as an example)

Make sure
image
Is in ReplicatedStorage aswell

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.