I need help with merging these scripts

G’day

I have 2 scripts that I want to merge into one

I have tried to merge these 2 scripts but I cant get it to work

script 1

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("GemSaveSystem")
local ds2 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
 local folder = Instance.new("Folder", plr)
 folder.Name = "leaderstats"
 local gems = Instance.new("IntValue", folder)
 gems.Name = "Gems"
 local cash = Instance.new("IntValue", folder)
 cash.Name = "Cash"
	
	
 gems.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, gems.Value)
 
 cash.Value = ds2:GetAsync(plr.UserId) or 0
 ds2:SetAsync(plr.UserId, cash.Value)
 
 gems.Changed:connect(function()
  ds1:SetAsync(plr.UserId, gems.Value)
 end)
 
 cash.Changed:connect(function()
  ds2:SetAsync(plr.UserId, cash.Value)
 end)
end)

script 2

local playerStats = {}

game.Players.PlayerAdded:connect(function(player)
local leader = Instance.new("Model", player)
leader.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Parent = player.leaderstats
cash.Name = "Cash"
cash.Value = 50
end)

while true do
for _, v in pairs(game.Players:GetPlayers()) do
v.leaderstats.Cash.Value = v.leaderstats.Cash.Value + 10
end
wait(30)
end


I would also like to remove the bit that says “gems” also

Something like this I believe is what you’d do if you want to combine your cash stuff and the loop

local datastore = game:GetService("DataStoreService")
local ds2 = datastore:GetDataStore("CashSaveSystem")

game.Players.PlayerAdded:connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "leaderstats"
	local cash = Instance.new("IntValue", folder)
	cash.Name = "Cash"

	cash.Value = ds2:GetAsync(plr.UserId) or 0
	ds2:SetAsync(plr.UserId, cash.Value)

	cash.Changed:Connect(function()
		ds2:SetAsync(plr.UserId, cash.Value)
	end)
end)

coroutine.wrap(function()
	while true do
		for _, v in pairs(game.Players:GetPlayers()) do
			v.leaderstats.Cash.Value = v.leaderstats.Cash.Value + 10
		end
		wait(30)
	end
end)()

Also I’d recommend only updating the datastore for a specific player on leave/server shutdown instead of when the value changes so you wont get warns about the queue, since you’d be sending a lot of set requests if you’re going to change the value a lot

By the way, You shouldn’t run a .Changed event and in it run a :SetAsync, Though I don’t know how you’re game is but whatever you do. If you’re going to have something like a clicking tool to give cash then run :SetAsync in a PlayerRemoving event.

1 Like

It was for a gui for buying in-game cash but I then had to merge it with a money script. It is now sorted