How to turn this currency script into two currenices?

First, let me explain.

I am currently making a game that heavily relies on currency items, and I wanted to make exclusive content for another currency. The main currency are jewels that you can obtain throughout the game, and there is another one called “Voids.”

Voids are used to purchase items that are exclusive to this type of currency, and not with jewels. However, I’m having a heavily hard time on trying to implement this into my game.

  1. What do you want to achieve?
  • I want to make two currencies work alongside each other without conflicting in-game GUI’s.
  1. What is the issue?
  • As stated above, I want to make two currencies work with each other, but they keep conflicting with one another. The two currencies keep conflicting one another, as shown in this GUI.
    image
  1. What solutions have you tried so far?
  • I have tried going onto multiple scripting forums, as well as going onto YouTube and trying to look up my problem. Nothing has worked and I am incredibly stumped. Additional help would be extremely appreciated.

I am new to scripting, so please try to keep it as simple as possible when explaining!

This is the Jewels currency, labeled as ‘Gold’ in the script to make it easier for me to remember.

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"
	
	if CashStore:GetAsync("Points_"..player.Name) ~= nil then
		cash.Value = CashStore:GetAsync("Points_"..player.Name)
	else
		cash.Value = 0
	end
	
	cash.Changed:connect(function(Val)
		CashStore:SetAsync("Points_"..player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)



This is the other script I’m trying to work with the Jewels currency, and I thought that it’d work- it didn’t.

VoidStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Voids"
	
	if VoidStore:GetAsync("Points_"..player.Name) ~= nil then
		cash.Value = VoidStore:GetAsync("Points_"..player.Name)
	else
		cash.Value = 0
	end
	
	cash.Changed:connect(function(Val)
		VoidStore:SetAsync("Points_"..player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)



Please, I am in dire need of help and assistance!

1 Like

One immediate problem I’ve spotted is that you’ve created leader stats twice, (once in each script). You need to create that only once and put the 2 IntValue instances into it.

See if that fixes it and let me know if it doesnt

1 Like

A bit confused as to how they’re conflicting…

I’ll let you know in a moment! Thanks!

I am not too sure if I have done this correctly, though now the currencies aren’t even saving properly. Any idea on what I did wrong?

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"
	
	local void = Instance.new("IntValue")
	void.Voids = "Voids"
	void.Value = 0
	
	if CashStore:GetAsync("Points_"..player.Name) ~= nil then
		cash.Value = CashStore:GetAsync("Points_"..player.Name)
		void.Value = CashStore:GetAsync("Points_"..player.Name)
	else
		cash.Value = 0
		void.Value = 0
	end
	cash.Changed:connect(function(Val)
	
		CashStore:SetAsync("Points_"..player.Name, Val)

	end)
	void.Changed:connect(function(Val)
		
		CashStore:SetAsync("Points_"..player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)



``

Its because “Points_” is used for Void and Gold

Change Points_ for void to “Void”
and Points_ for gold to “Gold”

I don’t work with data store often so I don’t know if you have to declare it first or if you can just declare it like vie done above. but try it anyway

(and obvs your going to need to split that if statement into 2 individual ones for Gold and Void)

1 Like

What do you mean by splitting the if statement into 2 individual ones? Like I said, I’m new to scripting so it can be a bit hard to follow along. My apologies.

1 Like

Apoogies, I ment just try this

if CashStore:GetAsync("GOLD"..player.Name) ~= nil then
	cash.Value = CashStore:GetAsync("GOLD"..player.Name)
else
	cash.Value = 0
end

if CashStore:GetAsync("VOID"..player.Name) ~= nil then
	void.Value = CashStore:GetAsync("VOID"..player.Name)
else
	void.Value = 0
end

cash.Changed:connect(function(Val)
	CashStore:SetAsync("GOLD"..player.Name, Val)
end)
void.Changed:connect(function(Val)
	CashStore:SetAsync("VOID"..player.Name, Val)
end)

Again someone may need to correct me if you have to Declare a variable in a datastore
I’m just assuming it works like Dictionarys

1 Like

It’s okay! Don’t worry about it!

What I’ve noticed after using your code is that the GUI’s are now pretty much glitched, unfortunately.

The Gold isn’t even being saved now, as it comes up as 0. The other currency is still N/A.
Example shown below:
image

This is what I did with the code after using your example.

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"
	
	local void = Instance.new("IntValue")
	void.Parent = stats
	void.Void = "Voids"
	
	if CashStore:GetAsync("Points_"..player.Name) ~= nil then
		if CashStore:GetAsync("GOLD"..player.Name) ~= nil then
			cash.Value = CashStore:GetAsync("GOLD"..player.Name)
		else
			cash.Value = 0
		end
		
		if CashStore:GetAsync("VOID"..player.Name) ~= nil then
			void.Value = CashStore:GetAsync("VOID"..player.Name)
		else
			void.Value = 0
		end

	end
	cash.Changed:connect(function(Val)
		CashStore:SetAsync("GOLD"..player.Name, Val)
	end)

	void.Changed:connect(function(Val)
		CashStore:SetAsync("VOID"..player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)



Are there any lines that I need to erase?

Edit
I have deleted some lines from the code, but yet it still doesn’t even work.

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character
	
	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"
	
	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"
	
	local void = Instance.new("IntValue")
	void.Parent = stats
	void.Void = "Voids"

		if CashStore:GetAsync("GOLD"..player.Name) ~= nil then
			cash.Value = CashStore:GetAsync("GOLD"..player.Name)
		else
			cash.Value = 0
		end
		
		if CashStore:GetAsync("VOID"..player.Name) ~= nil then
			void.Value = CashStore:GetAsync("VOID"..player.Name)
		else
			void.Value = 0
		end

	cash.Changed:connect(function(Val)
		CashStore:SetAsync("GOLD"..player.Name, Val)
	end)

	void.Changed:connect(function(Val)
		CashStore:SetAsync("VOID"..player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)


Is there something wrong with the code?

Okay I didn’t want to do it like this because i know its not the best way but i know it works

GoldStore = game:GetService("DataStoreService"):GetDataStore("DataStoreGold")
VoidStore = game:GetService("DataStoreService"):GetDataStore("DataStoreVoid")

function PlayerEntered(player)
	
	repeat wait() until player.Character

	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"

	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"

	local void = Instance.new("IntValue")
	void.Parent = stats
	void.Void = "Voids"

	if GoldStore:GetAsync(player.Name) ~= nil then
		cash.Value = GoldStore:GetAsync(player.Name)
	else
		cash.Value = 0
	end

	if VoidStore:GetAsync("VOID"..player.Name) ~= nil then
		void.Value = VoidStore:GetAsync(player.Name)
	else
		void.Value = 0
	end

	cash.Changed:connect(function(Val)
		GoldStore:SetAsync(player.Name, Val)
	end)

	void.Changed:connect(function(Val)
		VoidStore:SetAsync(player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

if this doesnt work it might just be a problem with the GUI
but hopefully someone ese can fix this code without having to create 2 datastores

1 Like

Sorry about all of that. :frowning: I really do appreciate all the help, I’m gonna try and use this script really quick! Thank you so much. I’ll use this as a way to learn more. :slight_smile:

If there is something wrong with the GUI’s, I’ll look into it - nonetheless, thank you so much!

One reason it might not have been working is because we were using new data stores. so its going to be 0 by defult but i dont know why the other ones showing n/a tho, make sure that ones linked to leaderstats.Gold.Value and the other one is leaderstats.Void.Value

1 Like

Thank you for all of your help, but unfortunately it’s still not working. I tweaked the GUI’s to match with the script you made, putting the script into the ServerScriptService, and allowing it to run on a test. Nothing didn’t work.

Again, thank you for all the help- this may help somebody if they were to search up what I was going through. : )

This doesn’t work because CashStore:GetAsync("Points_"..player.Name) ~= nil then is always nil because you never set “Points_” try removing that if statement with its end

1 Like

Okay! I’ll try that! Thank you.

It didn’t work, unfortunately. Thank you anyways!

Did you create something inside of the Voids? Are you sure you didn’t mean .Name?

void.Name = "Voids" -- This looks correct to me.

Your code should look something like this.

CashStore = game:GetService("DataStoreService"):GetDataStore("DataStore")

function PlayerEntered(player)
	repeat wait() until player.Character

	local stats = Instance.new("IntValue")
	stats.Parent = player
	stats.Name = "leaderstats"

	local cash = Instance.new("IntValue")
	cash.Parent = stats
	cash.Name = "Gold"

	local void = Instance.new("IntValue")
	void.Parent = stats
	void.Name = "Voids"

	if CashStore:GetAsync("GOLD"..player.Name) ~= nil then
		cash.Value = CashStore:GetAsync("GOLD"..player.Name)
	else
		cash.Value = 0
	end

	if CashStore:GetAsync("VOID"..player.Name) ~= nil then
		void.Value = CashStore:GetAsync("VOID"..player.Name)
	else
		void.Value = 0
	end

	cash.Changed:connect(function(Val)
		CashStore:SetAsync("GOLD"..player.Name, Val)
	end)

	void.Changed:connect(function(Val)
		CashStore:SetAsync("VOID"..player.Name, Val)
	end)
end

game.Players.PlayerAdded:connect(PlayerEntered)

I would recommend that you save data using player.UserId instead of player.Name. Players can change their usernames and would lose their data. I didn’t change it for the purpose of keeping your experience working. There are lots of things I would change in this script. However, it might go beyond your topic. If you would like me to give you more advice, send me a message on DevForum.

2 Likes

On top of this, in the future I would look into saving players data in a table when using roblox data stores, this is more efficient and will result in less data store requests entering the queue which could result in some users losing their currency.

2 Likes

This did the trick for me! Oh my god, thank you so much!

2 Likes

I’ll keep all of this in mind when I’m continuing to script my game, thank you so much!

1 Like