Adding currency with Gui

I’m trying to create a button that gives you money when you click it but it doesnt work i know its possible to do it with remote events but if i want to have a lot of them i need to make so manny remote events isnt there a way i can just do this all in the script ive tried this but this doesnt work btw its a local script inside of the button. please tell me if there’s a way!

wait(5)
local moneyButton = script.Parent
local minValue = script.Parent.MinimumValue
local maxValue = script.Parent.MaximumValue
local npcDialogueFrame = script.Parent.Parent
local values = script.Parent.Parent.Parent.Parent.Values
local moneyMultiplier = values.MoneyMultiplier

moneyButton.MouseButton1Up:Connect(function()
	local player = game.Players.LocalPlayer
	local money = player:WaitForChild("leaderstats"):WaitForChild("Money")
	
	local randomAmount = math.random(minValue.Value, maxValue.Value)
	local randomAmountMultiplied = randomAmount * moneyMultiplier.Value
	money.Value += randomAmountMultiplied
	
	npcDialogueFrame.Visible = false
end)
1 Like

You can just make one remote event and send the money value to that remote event through multiple scripts…

is there also a way with out remote events?

Maybe there is, is the script you provided a server script or a local script?

local script in a button dddddd

I don’t think this part is necessary, remove it and your script would work

thats just so everyting loads _________

Still, it causes the script to not work… It’s not really necessary…

You will need to use a RemoteEvent if you are using GuiButtons.

it still doesnt give an error it does cloeses the gui at the end but gives no money

An arbitrary wait is extremely unnecessary. Since the script is a child of the ScreenGui, there should be no issue of things not loading unless if you add them later; however, you should use WaitForChild in that scenario rather than an abritrary wait.

Are you using any DataStores to save players’ data? If yes, then using RemoteEvent is a good solution. Can you give me the reason that you want to avoid using RemoteEvent?

Then use a remote event just like what @COUNTYL1MITS said

ok ill try the remote events but i have no idea how so thats why i dint want to use can you maybe show me

Ok.
Example:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
local button = script.Parent
button.MouseButton1Click:Connect(function()
    local amount = 100
    event:FireServer(amount)
end)

On the server…

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
event.OnServerEvent:Connect(function(plr,amount)
    print(amount) -- prints 100
end)

Take a look at this page and the page I sent above in a previous reply.

I think this will work thank you but how can i add the money to the player ive tried this but, it doesnt work do you know how maybe?

	player.leaderstats.Money.Value += amount

Client:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
local button = script.Parent
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local leaderstats = player:WaitForChild("leaderstats")
local money = leaderstats:WaitForChild("Money")
button.MouseButton1Click:Connect(function()
    local amount = 100
    event:FireServer(amount,money)
end)

Server:

local event = game:GetService("ReplicatedStorage"):WaitForChild("Event")
event.OnServerEvent:Connect(function(plr,amount,money)
    print(amount) -- prints 100
    money.Value += amount
end)

It would be a better idea to define the amount on the server rather than have the client send it over as it can be very exploitable.

I did, but thanks for the tip.