Money Exchanger not working

Hello again! Today I’m making a cash exchanger (like Cash to Gems)
but it doesn’t work…

here’s the script:

local plr = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local Data = plr:WaitForChild("Data")
local Currency = Data.Currency
local CashValue = Currency.CashValue
local ResearchPoints = Currency.ResearchPoints

local Funds = script.Parent -- Button
local Int = Funds.Parent.Value -- IntValue

Funds.MouseButton1Click:Connect(function()
	if CashValue.Value >= Int.Value then
		CashValue.Value = CashValue.Value - Int.Value
		ResearchPoints.Value = ResearchPoints.Value + Int.Value * 5
	end
end)

all helps are appreciated.

I don’tthink you need the GetPropertyChangedSignal event i nthe Mousebutton1Click event for this. Also, I recommend you try to convert the text to a number first and then do the conversion. Also Amount.Value is going to error since Textboxs don’t have the Value property, try this out?

Funds.MouseButton1Click:Connect(function()
	local convertAmount = tonumber(Amount.Text)
	if not convertAmount then return end --Text wasn't a number
	if Cash.Value >= convertAmount then
		Cash.Value -= convertAmount
		ResearchPoints.Value += (convertAmount * 5)
	end
end)

Also not sure if you forgot to put .Value after Cash since I’m not sure if it’s a BaseValue or not

1 Like

thanks! but I tried modifying it multiple times and it didn’t work… :frowning:

Try adding print statements to see where it is stopping? And try printing convertAmount after setting it. It should work, somewhere in there it is stopping

Is this a Script or a Local Script?

Well actually, that was a localscript.

If it is a local script then Fire a RemoteEvent when the player clicks the button and do the calculation/add value to the player on the server.

Uh oh, I wasn’t the best at RemoteEvent but i’ll try.

Here i will give you an example

Local Script

local remote = game.ReplicatedStorage.RemoteEvent

script.Parent.MouseButton1Down:Connect(function()
      remote:FireServer()
end)

Script

local remote = game.ReplicatedStorage.RemoteEvent

remote.OnServerEvent:Connect(function(plr)
   --Do stuff
end)

Make sure to add sanity checks so exploiters would not take advantage of this!

so, what if its a script? I tried with RemoteEvent but it doesn’t work

The only way to do this is by detecting mouse clicks on the client and doing everything else on the server as the client cannot be trusted. See my previous reply for an example and make sure that you are doing it right.

The only major error I see is that you are trying to check if Cash >= String, which won’t work.
Try this instead, since you need to compare it with an int:

if Cash >= tonumber(Amount.Text) then

if Amount.Value is a string as well, use tonumber as shown above.

Also, your second function (Amount:GetPropertyChangedSignal("Text")) shouldn’t be inside of your MouseButton1Click event as it will only fire once the player fires the first event.

Also, I don’t see why you are doing this client-sided as it won’t replicate (update) on the server-side.
And if you plan to switch to a RemoteEvent, DO NOT check the cash amount on the client as exploiters can have a fun time if that’s the case.

What happens when i switched it to a server script?

UI-related things should not be handled by the server.

aight, I think I’ll use another way instead of textboxes.

You can use whatever you want, just don’t let exploiters take advantage of it :slight_smile:

also, I kinda redo it and it’s still not working :frowning: