Screwed up, pretty bad

So my idiot self was writing a leaderstats script for a money system, you know, the usual. Then finished that and worked on a shop to spend the money at and I wrote the WHOLE thing (Except for the actual intvalue, only the shop.) in a LOCAL SCRIPT!!! So now whenever you spend money the server is like, “Bruv i do not care” and doesnt update. You would think I could just move some of it over to a normal script but it is a big script and I dont know how to fix it without re writing the whole thing, or if its even possible. HELP!!

script: (all local)

--|MoneyStuff|--
local LocalPlayer = game.Players.LocalPlayer
local leaderstats = LocalPlayer:WaitForChild("leaderstats")
local IntValue = leaderstats.Money
--|MainFrame|--
local MainFrame = script.Parent.MainFrame
--|DiaLogBox|--
local DialogBox = MainFrame.DialogBox
local BuyButton = DialogBox.Buy
local SellButton = DialogBox.Sell
--|BuyBox|--
local BuyBox = MainFrame.Buy
local BuyPizza = BuyBox.Pizza
local BuyWater = BuyBox.Water
--|Other|--
local LocalPlayer = game.Players.LocalPlayer
local DB = false
local rep = game:GetService("ReplicatedStorage")
local PizzaTool = rep.PizzaTool
local WaterTool = rep.WaterTool
local X = MainFrame.X
local ProxEvent = rep.ShopProxToggle

local ProximityPrompt = game.Workspace:WaitForChild("ProximityBox").ProximityPrompt
local DB = false

ProximityPrompt.Triggered:Connect(function(player)
	
	wait(.1)
	ProximityPrompt.Enabled = false
	MainFrame.Visible = true

end)

BuyButton.MouseButton1Click:Connect(function(Player)
	if not DB then
	DB = true
	DialogBox.Visible = false
	BuyBox.Visible = true
		wait(1)
	DB = false	
	end
end)

BuyPizza.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		if IntValue.Value >= 5 then
			IntValue.Value = IntValue.Value - 5
			local NewPizza = PizzaTool:Clone()
			NewPizza.Parent = LocalPlayer.Backpack
			print("Bought")
		end
		    wait(1)
		DB = false
	end
end)

BuyWater.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		if IntValue.Value >= 2 then
			IntValue.Value = IntValue.Value - 2
			local NewWater = WaterTool:Clone()
			NewWater.Parent = LocalPlayer.Backpack
			print("Bought")
		end
		wait(1)
		DB = false
	end
end)

X.MouseButton1Click:Connect(function(Player)
	if not DB then
	DB = true
DialogBox.Visible = true	
BuyBox.Visible = false
MainFrame.Visible = false
	ProxEvent:FireServer(Player)
	ProximityPrompt.Enabled = true
		wait(1)
	DB = false
	end
end)

How do I make it so the money actual updates. Please tell me I dont have to rewrite this thing.

4 Likes

Whenever someone clicks on one of the items, fire a remote event to the server with the item they clicked on and server-side check if the player has enough money, and if they do, add it to their backpack. So instead of checking here if they have the money on the client, you can just do,

--LocalScript
BuyWater.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		PurchaseItemEvent:FireServer("Water")
		wait(1)
		DB = false
	end
end)
--ServerScript
local items = {
Water = 5,
Pizza = 10,
}

PurchaseItemEvent.OnServerEvent:Connect(function(item)
    if items[item] and Player.leaderstats.Money.Value >= items[item]  then
       Player.leaderstats.Money.Value = Player.leaderstats.Money.Value - items[item]
       -- Do whatever else here
    end
end)

You’ll have to reference the RemoteEvent in the scripts and you might have to modify what I gave here, to fit your needs.

2 Likes

How do I reference the Player in the server script?

1 Like

When a RemoteEvent is fired the first argument is always the player who fired it:

RemoteEvent.OnServerEvent:Connect(function(player: Player, yourArgsHere)
  -- Your code here
end)

This is the server script:

local rep = game:GetService("ReplicatedStorage")
local PurchaseItemEvent = rep.ItemPurchased
local WaterTool = rep.WaterTool

local items = {
	Water = 5,
	Pizza = 10,
}

PurchaseItemEvent.OnServerEvent:Connect(function(player, item)
	if items[item] and player.leaderstats.Money.Value >= items[item]  then
		player.leaderstats.Money.Value = player.leaderstats.Money.Value - items[item]
		local NewWater = WaterTool:Clone()
		NewWater.Parent = player.Backpack
	end
end)

Does not work.

2 Likes

What doesn’t work about it? Did you fire an event from the client? Did you have enough money to purchase the items to begin with?

2 Likes

So I used print() and it seems that the “if statement” in the server script is the thing that prevents it from working. You see anything wrong?

1 Like

Can we see the client script to see what is being sent to the server?

1 Like
BuyWater.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		PurchaseItemEvent:FireServer("Water")
		wait(1)
		DB = false
	end
end)

This isnt all of it but it is the only thing that matters rn.
Also it is in starter gui.
variables:

--|MoneyStuff|--
local LocalPlayer = game.Players.LocalPlayer
local leaderstats = LocalPlayer:WaitForChild("leaderstats")
local IntValue = leaderstats.Money
--|MainFrame|--
local MainFrame = script.Parent.MainFrame
--|DiaLogBox|--
local DialogBox = MainFrame.DialogBox
local BuyButton = DialogBox.Buy
local SellButton = DialogBox.Sell
--|BuyBox|--
local BuyBox = MainFrame.Buy
local BuyPizza = BuyBox.Pizza
local BuyWater = BuyBox.Water
--|Other|--
local LocalPlayer = game.Players.LocalPlayer
local DB = false
local rep = game:GetService("ReplicatedStorage")
local PizzaTool = rep.PizzaTool
local WaterTool = rep.WaterTool
local X = MainFrame.X
local ProxEvent = rep.ShopProxToggle
local PurchaseItemEvent = rep.ItemPurchased
local ProximityPrompt = game.Workspace:WaitForChild("ProximityBox").ProximityPrompt
local DB = false
1 Like

Everything should work then, ensure the ServerScript is inside of ServerScriptService. If it continues to not work, then you must not have enough money to purchase the item, cause I just tested it with the exact same code.

1 Like

Well that might be it, not the money, but where the script is. It is in the Starter Gui but when I move it to server script service it does not work. btw it is a local script.

1 Like

make sure when you put the serverscriptservice service the actual script is not a local script. When I was making a shop I basically had the same problem.

also, this is not really important… but one thing that I did was when I’d fireServer in the local script I also passed in the cost and it was very easy to subtract money from the cost.

hope this helps :slight_smile:

1 Like

But if it is a script then the gui will not work, it has to be a local script.

Local script right now:

I put this whole thing in server script service and make it a script, and the gui does not work.

--|MainFrame|--
local MainFrame = script.Parent.MainFrame
--|DiaLogBox|--
local DialogBox = MainFrame.DialogBox
local BuyButton = DialogBox.Buy
local SellButton = DialogBox.Sell
--|BuyBox|--
local BuyBox = MainFrame.Buy
local BuyPizza = BuyBox.Pizza
local BuyWater = BuyBox.Water
--|Other|--
local LocalPlayer = game.Players.LocalPlayer
local DB = false
local rep = game:GetService("ReplicatedStorage")
local PizzaTool = rep.PizzaTool
local WaterTool = rep.WaterTool
local X = MainFrame.X
local ProxEvent = rep.ShopProxToggle
local PurchaseItemEvent = rep.ItemPurchased
local ProximityPrompt = game.Workspace:WaitForChild("ProximityBox").ProximityPrompt
local DB = false

ProximityPrompt.Triggered:Connect(function(player)

	wait(.1)
	ProximityPrompt.Enabled = false
	MainFrame.Visible = true

end)

BuyButton.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		DialogBox.Visible = false
		BuyBox.Visible = true
		wait(1)
		DB = false	
	end
end)

BuyPizza.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		PurchaseItemEvent:FireServer("Pizza")
		print("Bought")
		wait(1)
		DB = false
	end
end)

BuyWater.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		PurchaseItemEvent:FireServer("Water")
		wait(1)
		DB = false
	end
end)

X.MouseButton1Click:Connect(function(Player)
	if not DB then
		DB = true
		DialogBox.Visible = true	
		BuyBox.Visible = false
		MainFrame.Visible = false
		ProxEvent:FireServer(Player)
		ProximityPrompt.Enabled = true
		wait(1)
		DB = false
	end
end)
1 Like

Don’t put the whole code inside the server script.

What you want to have, and how I did it was when I made something similar is basically 2 scripts. 1: The Server script which will be what will use the FireServer(), deducts money and gives the player the item. 2: the local script which will FireServer() with the item. And this is the idea that @Tylerisawsome113 was suggesting.

A way you could do this is when the player has enough money you can do this on the local script in the Gui

BuyWater.MouseButton1Click:Connect(function()
	if not DB then
		DB = true
		PurchaseItemEvent:FireServer(Water) -- change this to be whatever item you want to be purchased
		wait(1)
		DB = false
	end
end)

And then the server script, that is in ServerScriptService.

local rep = game:GetService("ReplicatedStorage")
local PurchaseItemEvent = rep.ItemPurchased
local WaterTool = rep.WaterTool

local items = {
	Water = 5,
	Pizza = 10,
}

PurchaseItemEvent.OnServerEvent:Connect(function(player, item)
	if items[item] then
		local cost = items[item]
		if player.leaderstats.Money.Value >= cost then
			player.leaderstats.Money.Value = player.leaderstats.Money.Value - cost
			WaterTool:Clone().Parent = player.Backpack
		end
	end
end)
1 Like

@ JojoKujo This is 2 scripts …
The top script is a simple click fire event that would be in your GUI triggered off a buy button.
That is a local script. The lower script is a non-local script in ServerScriptService
There should be a RemoteEvent (add from studio) for each FireServer (item) in ReplicatedStorage.
As a non-local script and away from hackers that script is taking care of the money.
And will also clone the item from → ServerStorage ← to the players backpack.
(maybe you can do it from ReplicatedStorage, idk)

2 Likes

It works but, when you buy pizza you also get water, how would I give the player multiple options?