Currency1 -> Currency2 Conversion

I have 2 currencies in my game, those being tickets and money. I have a ScreenGui that has a TextBox and a TextButton that allows the player to enter a custom amount of tickets to cash out (if the player owns the amount they have typed in the textbox of course)
The tickets are converted on a rate of 1:2 (1 ticket = 2 money).

The only problem is, I don’t have any idea on how to make it work…

This is what I have tried (It is placed inside the TextButton, in a server script, not local):

button = script.Parent

button.Activated:Connect(function(plr)
	local number = button.Text
	plr.Tickets.Value -= number
	plr.leaderstats.Money.Value += number
end)

(it prints no errors either)
Any sort of help is greatly appreciated.

1 Like

It would be in your best interest to fire a RemoteEvent when the player clicks the text button. Your code for detecting when the text button is clicked should be a local script in StarterCharacterScripts that fires a remote event, from there you can handle everything on the server.

1 Like

You’ll need to use a remote event. The button event isn’t picked by server side.

  1. Create a remote event in replicated storage, and give it a name like ConvertCurrency.
  2. add this to a client script:
button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")

button.Activated:Connect(function(plr)
	local number = button.Text
	ReplicatedStorage.ConvertCurrency:FireServer(tonumber(button.Text))
end)
  1. Create a script in server script service, with this:
local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.ConvertCurrency.OnServerEvent:Connect(function(plr, number)
    if plr.Tickets.Value < number then return end
    plr.Tickets.Value -= number
    plr.leaderstats.Money.Value += number
end)

I hope this works for you! good luck!

1 Like

I have tried your solution but it gave me this error:
ServerScriptService.ConvertTickets:4: attempt to compare number < nil

change line 4 in the serverscript to this:

    if plr.Tickets.Value < tonumber(number) then return end
1 Like

also in the local script he made a mistake, change line 6 in the local script to this:

	ReplicatedStorage.ConvertCurrency:FireServer(number)
1 Like

It prints out the same error:
ServerScriptService.ConvertTickets:4: attempt to compare number < nil

What does the text on the textbutton say?

Nevermind, I fixed it, It didn’t mention the textbox correctly
This is the working script (had to change line 5 in local script):

button = script.Parent
local ReplicatedStorage = game:GetService("ReplicatedStorage")

button.Activated:Connect(function(plr)
	local number = button.Parent.TextBox.Text
	ReplicatedStorage.Remotes.ConvertTicketsRemote:FireServer(number)

end)
1 Like

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