Money gui does not update

buy button script:

local shopframe=script.Parent.Parent.ShopScrollFrame
local buybutton=script.Parent
local money=game.Players.LocalPlayer:WaitForChild("leaderstats").Money.Value
for i,v in pairs(shopframe:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		if v.Name=="BarrierButton" then
			buybutton.Text=tostring(shopframe.BarrierButton.Price.Value)
			buybutton.MouseButton1Click:Connect(function()
				money=money-v.Price.Value
			end)
		elseif v.Name=="SpeedButton" then
			buybutton.Text=tostring(shopframe.SpeedButton.Price.Value)
			buybutton.MouseButton1Click:Connect(function()
				money=money-v.Price.Value
			end)
		elseif v.Name=="JumpButton" then
			buybutton.Text=tostring(shopframe.JumpButton.Price.Value)
			buybutton.MouseButton1Click:Connect(function()
				money=money-v.Price.Value
			end)
		end
	end)
end

money display gui:

local leaderstats=game.Players.LocalPlayer:WaitForChild("leaderstats")
local money=leaderstats.Money
while true do
	wait()
	
	script.Parent.Text="$"..money.Value
end

Leaderstats

game.Players.PlayerAdded:Connect(function(player)
	local leaderstats= Instance.new("Folder")
	leaderstats.Name= ("leaderstats")
	leaderstats.Parent= player
	
	
	local cash= Instance.new("NumberValue")
	cash.Name= ("Money")
	cash.Parent = leaderstats
	cash.Value = 500 
	
	
end)

so basically, I have a shop gui. The buy button is supposed to subtract the leaderstat value from the price value. All are numbervalues. It returns no errors, but it does not work.

6 Likes

you need to use a remote event because all ur doing is changing it from the client which does not replicate over to the server

1 Like

do you know how to do it? Im not that familiar with remote events and functions.

1 Like

You can use remote:FireServer() to trigger the remote event.

i’m not sure what your explorer looks like. could you please send it?

1 Like


gui of shop. The leaderstats is a script located in serverscriptservice.

1 Like

If you want the basics of RemoteEvents basically when you fire a RemoteEvent from a local script it will fire it on the server which is the only choice you have from the client. If you fire a RemoveEvent from the server you will have the choice to decide on which client the event will be fired on or you can chose to it sends to all clients aka all the players. Here’s a few pictures to give you an idea how it works.



Screenshot (129)

Note that the same RemoteEvent can work both ways so from Server > Client(s) and Client > Server.

If you need more help to understand how it works and it’s purpose there’s the link to the documentation.

2 Likes

I figured it out, but for some reason, the values add up when I click again.

Serversciptservice script:

local event =game:GetService("ReplicatedStorage").UpdateMoney
event.OnServerEvent:Connect(function(player, money)
	money=tonumber(money)
	
		print("money lol")
		local leaderstats = player:WaitForChild("leaderstats")
		local cash = leaderstats:FindFirstChild("Money")
		if cash then
		cash.Value=cash.Value-money
		
	end

end)

buy button script:

local shopframe=script.Parent.Parent.ShopScrollFrame
local buybutton=script.Parent
local event=game:GetService("ReplicatedStorage").UpdateMoney
local money=game.Players.LocalPlayer:WaitForChild("leaderstats").Money.Value
for i,v in pairs(shopframe:GetChildren()) do
	v.MouseButton1Click:Connect(function()
		if v.Name=="BarrierButton" then
			buybutton.Text=tostring(shopframe.BarrierButton.Price.Value)
			buybutton.MouseButton1Click:Connect(function()
				
				event:FireServer(10)
			end)
		elseif v.Name=="SpeedButton" then
			buybutton.Text=tostring(shopframe.SpeedButton.Price.Value)
			buybutton.MouseButton1Click:Connect(function()
				money=money-v.Price.Value
				event:FireServer(30)
			end)
		elseif v.Name=="JumpButton" then
			buybutton.Text=tostring(shopframe.JumpButton.Price.Value)
			buybutton.MouseButton1Click:Connect(function()
				money=money-v.Price.Value
				event:FireServer(60)
			end)
		end
	end)
end

remotevent is in replicated storage.

Do NOT do this. Roblox added new operators specifically for this purpose a while ago, so now instead of money = money -
you can now use:
money -=
to save time!

As for it not updating, that’s because you can’t change values on the client. I’d use ReflexModule (But it might be too advanced) OR a remoteevent

Oh, I didn’t even know that. I have figured it out by using remoteevents, but for some reason, in the post above, when I fire the events, the numbers add up. For example, if I purchase part A for 10 cash, the event will fire as 10. But if I purchase another part named part B for 20 cash, the event will fire as 30. (10+20=30) So idk how to make the previous event cancel.

This is because you’re creating a mousebutton1click event for EVERYTHING in the shopframe gui

So if you have 3 items in the shopframe gui, it will create 3 events, but play them at the same time when you click it. I BELIEVE this is the error. (Not to mention, you’re binding all of them to buybutton, so if someone clicks speedbutton, then clicks jumpbutton, they’ll both bind to buybutton and it’ll buy both)

Do you know any way to bind it to buybutton (because I want it to be optimized, since there will be many items in the shop, and having many buybuttons will not be organized)but also make the events not bind to the button.

yt video to help you:remoteevent being weird - YouTube

I don’t know if it may help, but you can use v.MouseButton1Click:Once(function()

It works only one time, unfortunately.

(it seems like every shop tutorial uses seperate events, may consider use that)

Sorry, change that back to connect. What you should be changing to use :Once() are the buybutton events

Change the connect to once for all of these, and it should work ish.

1 Like

Yo, thanks man! This really helped me a lot.

You’re welcome! If you have any more problems send me a message.

1 Like

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