Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

DataStore not saving currencies?

Hello! I have tried to create a double-currency system, with two different kinds of currencies but in the same script. As of now, I attempted to make the first one work due to not being loaded when the game starts, even if it saves. If you could, I would also love if you helped me in making another currency in the same script, thank you.

local currencyName = "¥"
local DataStore = game:GetService("DataStoreService"):GetDataStore("MoneyDataStore")

game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = player
	
	local currency = Instance.new("IntValue")
	currency.Name = currencyName
	currency.Parent = folder
	
	local playerID = ""..player.UserId
	local savedData = nil
	
	local savedData = DataStore:GetAsync(playerID)
	
	if savedData ~= nil then
		print("loaded"..currency.Value)
		currency.Value = savedData
	else
		print("hi")
		currency.Value = 0
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerID = currencyName.."coin-"..player.UserId
	DataStore:SetAsync(playerID, player.leaderstats[currencyName].Value)
	print("saved "..player.leaderstats[currencyName].Value)
end)

game:BindToClose(function()
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			player:Kick()
		end
	end
	
	wait(5)
end)

4 answers

although i cant help you with fixing that script i can give you another script that does the same thing, just change the names:

local DSS = game:GetService("DataStoreService")
local PlayerData = DSS:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(Player)
	local StringData = PlayerData:GetAsync(Player.UserId) or 0
	local DataSplit = string.split(StringData, "+")
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	local Coins = Instance.new("NumberValue")
	Coins.Parent = leaderstats
	Coins.Name = "Coins"
	Coins.Value = DataSplit[1] or 0
	local Multiplier = Instance.new("NumberValue")
	Multiplier.Parent = leaderstats
	Multiplier.Name = "Multiplier"
	Multiplier.Value = DataSplit[2] or 1
	local Rebirths = Instance.new("NumberValue")
	Rebirths.Parent = leaderstats
	Rebirths.Name = "Rebirths"
	Rebirths.Value = DataSplit[3] or 1
end)
game.Players.PlayerRemoving:Connect(function(Player)
	PlayerData:SetAsync(Player.UserId, Player.leaderstats.Coins.Value.. "+".. Player.leaderstats.Multiplier.Value.. "+".. Player.leaderstats.Rebirths.Value)
end)

Put this script in your stats script

local dataStores = game:GetService("DataStoreService"):GetDataStore("CoinsDataStore")
local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)
	
	playersLeft = playersLeft + 1
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Value = 0
	coins.Parent = leaderstats
	
	local wins = Instance.new("IntValue")
	wins.Name = "Wins"
	wins.Value = 0
	wins.Parent = leaderstats
		
	end)
	
	-- Data stores
	
	local player_data
	
	pcall(function()
		player_data = dataStores:GetAsync(player.UserId.."-Coins")
	end)
	
	if player_data ~= nil then
		-- Player has saved data load it in
		coins.Value = player_data
	else
		-- New player
	end
	
end)

local bindableEvent = Instance.new("BindableEvent")

game.Players.PlayerRemoving:Connect(function(player)
	pcall(function()
		dataStores:SetAsync(player.UserId.."-Coins",player.leaderstats.Coins.Value)
		print("Saved!")
		playersLeft = playersLeft - 1
		bindableEvent:Fire()
	end)
	
end)

game:BindToClose(function()
	while playersLeft > 0 do
		bindableEvent.Event:Wait()
	end
end)

4 comments

0

You forgot to create the two values to save, I think, lmao

0

Unfortunately it doesn't seem to be working, I will answer to my post with the script I came up with.

0

Unfortunately it doesn't seem to be working, I will answer to my post with the script I came up with.

0

Put it into a Pcall function.


local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	wait()

	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr

	local currency = Instance.new("IntValue")
	currency.Name = "YEN"
	currency.Parent = plr.leaderstats

	local currency = Instance.new("IntValue")
	currency.Name = "MON"
	currency.Parent = plr.leaderstats

	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.YEN
	local save2 = plr.leaderstats.MON

	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, {plr.leaderstats.YEN.Value, plr.leaderstats.MON.Value})
end)

Hmm idk if this is the cause but in lines 15 and 17 you're putting local for both, it should be phrased as

local savedData -- you can put = nil here
savedData = DataStore:GetAsync(playerID)

that's all I have right now..

This discussion is preserved as it was. New answers, comments, and votes are closed.