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.
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.
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
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.