How do I make one currency equal 50 currency?

Read this thoroughly before you say it doesn’t make sense, I’m horrible at explaining :laughing:

So I want to make one brain token in our game equal 50 cash when they talk to the npc that converts them to cash. How could I do this? I’m not very familiar with how math in lua (or math at all, really) works…

And if the user had something, like, 5 brain tokens, they would get 250 cash. So on and so on.

Code so far:

script.Parent.DialogChoiceSelected:connect(function(player,choice)
	print(player.Name.. " chose ".. choice.Name)
	if choice.Name == "Convert" then
		if player.leaderstats["Brain Tokens"].Value > 0 then
			
		end
	end
end)

Well, since you can assume 1 token is 50 cash, then just multiply the value of tokens by 50, then add that sum onto the players cash leaderstat.

value * 50

So 1 token would be 50, 2 would be 100, 3 tokens is 150, etc

1 Like
local tokens = player.leaderstats["Brain Tokens"]
if tokens and tokens.Value > 0 then 
	local cash = tokens*50 
	--add the cash to their current currency
	local Success, Error = pcall(function() 
		player.leaderstats.Cash.Value += cash 
		--replace with path of cash value
	end)
	if Success then 
		--set their tokens to 0 ONLY if the purchase was successful
		tokens = 0 
	end
end 
2 Likes

Pretty simple, variables will be needed, I just did some that explain it
conversion = braintokens * 50
cash = cash + conversion

1 Like

It’s pretty simple once you understand it, assuming you have cash in the player’s leaderstats already this script is going to do what you are trying to say.

script.Parent.DialogChoiceSelected:connect(function(player,choice)
	print(player.Name.. " chose ".. choice.Name)
	if choice.Name == "Convert" then
		if player.leaderstats["Brain Tokens"].Value > 0 then
			local cash = player.leaderstats:FindFirstChild("Cash")
			if cash == nil then return end
			player.leaderstats.Cash.Value = player.leaderstats:FindFirstChild("Brain Tokens").Value *50 -- 50 times brain tokens value equals cash value
		end
	end
end)
1 Like

Cash hasn’t been defined in the provided solutions but I’m assuming it would be a leaderstat along with “Brain Tokens”, also I’m not sure why that one post used pcall() as it wouldn’t really be necessary in this circumstance.

script.Parent.DialogChoiceSelected:connect(function(player,choice)
	local leaderstats = player:WaitForChild("leaderstats") --load player's leaderstats
	local brainTokens = leaderstats:WaitForChild("Brain Tokens")
	local cash = leaderstats:WaitForChild("Cash")
	if choice.Name == "Convert" then --check choice
		cash.Value = brainTokens.Value * 50 --set cash to number of braintokens times 50
		brainTokens.Value = 0 --reset back to 0 after conversion
	end
end)
2 Likes

Hello,
Yeah I noticed that… quickly. I’m not sure why I made it the solution but it did partially help me with the math (so did the other comments).

I used proximity prompts instead by the way because we all don’t know that dialogues are client sided… yeah that sucks.

Here’s my final code though, I won’t mark anything here as a solution because technically it requires remote events, but I basically gave up on the dialogue:

local ProximityPrompt = script.Parent

ProximityPrompt.Triggered:connect(function(player)
	local tokens = player.leaderstats:WaitForChild("Brain Tokens")
	if tokens.Value > 0 then 
		player.leaderstats.Cash.Value += tokens.Value*50 
		tokens.Value = 0 
	end 
end)
2 Likes