Problem with DataStore

Hi guys I have written a small leaderboard here which should normally store the data and output it again. Problem is that it does not do this and does not give an error message. It would be a dream if you could help me with this problem.

local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr
	local stages = Instance.new("IntValue")
	stages.Name = "Stage"
	stages.Parent = folder
	stages.Value = 1
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = folder
	local megacoins = Instance.new("IntValue")
	megacoins.Name = "MegaCoins"
	megacoins.Parent = folder
	local checki = Instance.new("IntValue")
	checki.Name = "mglook"
	checki.Parent = folder
	if checki == 1 then
		print("7")
		plr.PlayerGui.stats.MegaCoins.Visible = true
	end
	coins.Changed:Connect(function(val)
		if coins.Value >= 1000000 then
			-- Do stuff.
			plr.PlayerGui.stats.MegaCoins.Visible = true
			megacoins.Value = megacoins.Value + 1
			coins.Value = 0
			checki.Value = 1
		end
	end)
	local coinsStored
	local megacoinsStored
	local checkiStored
	local success, errorMessage = pcall(function()
		coinsStored = DataStore:GetAsync(plr.UserId.."coins")
		megacoinsStored = DataStore:GetAsync(plr.UserId.."megacoins")
		checkiStored = DataStore:GetAsync(plr.UserId.."checki")
	end)
	if coinsStored ~= nil then
		coins.Value = coinsStored
	else
		coins.Value = 0
	end
	if megacoinsStored ~= nil then
		megacoins.Value = megacoinsStored
	else
		megacoins.Value = 0
	end
	if checkiStored ~= nil then
		checki.Value = checkiStored
	else
		checki.Value = 0
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	local success, errorMessage = pcall(function()
		DataStore:SetAsync(plr.UserId.."coins",plr.leaderstats.Coins.Value)
		DataStore:SetAsync(plr.UserId.."megacoins",plr.leaderstats.MegaCoins.Value)
		DataStore:SetAsync(plr.UserId.."checki",plr.leaderstats.mglook.Value)
	end)
end)
2 Likes
local DataStore = game:GetService("DataStoreService"):GetDataStore("MyDataStore")
local function ds(plr)--used to save data only need the player
	local savetable = { 
		plr.leaderstats.Coins.Value;
		plr.leaderstats.MegaCoins.Value;
		plr.leaderstats.mglook.Value
	} --the table to save to datastore.

	local success,errorMessage = pcall(DataStore.SetAsync,DataStore,plr.UserId,savetable) --saves the table to datastore

	if success then --optional but prints the state.
		print("Data of player "..plr.UserId.." has been saved.")
	else
		print("Failed to save the data of the player"..plr.UserId..".")
	end
end
game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder")
	folder.Name = "leaderstats"
	folder.Parent = plr
	local stages = Instance.new("IntValue")
	stages.Name = "Stage"
	stages.Parent = folder
	stages.Value = 1
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = folder
	local megacoins = Instance.new("IntValue")
	megacoins.Name = "MegaCoins"
	megacoins.Parent = folder
	local checki = Instance.new("IntValue")
	checki.Name = "mglook"
	checki.Parent = folder
	
	local data = nil
	
	local success, errorMessage = pcall(function()
		data = DataStore:GetAsync(plr.UserId)
	end)
	
	if data and success then
		coins.Value = data[1]
		megacoins.Value = data[2]
		checki.Value = data[3]
	end
	
	if checki == 1 then
		print("7")
		plr.PlayerGui.stats.MegaCoins.Visible = true
	end
	coins.Changed:Connect(function(val)
		if coins.Value >= 1000000 then
			-- Do stuff.
			plr.PlayerGui.stats.MegaCoins.Visible = true
			megacoins.Value = megacoins.Value + 1
			coins.Value = 0
			checki.Value = 1
		end
	end)
	local coinsStored
	local megacoinsStored
	local checkiStored
	local success, errorMessage = pcall(function()
		coinsStored = DataStore:GetAsync(plr.UserId.."coins")
		megacoinsStored = DataStore:GetAsync(plr.UserId.."megacoins")
		checkiStored = DataStore:GetAsync(plr.UserId.."checki")
	end)
	if coinsStored ~= nil then
		coins.Value = coinsStored
	else
		coins.Value = 0
	end
	if megacoinsStored ~= nil then
		megacoins.Value = megacoinsStored
	else
		megacoins.Value = 0
	end
	if checkiStored ~= nil then
		checki.Value = checkiStored
	else
		checki.Value = 0
	end
end)
game.Players.PlayerRemoving:Connect(function(plr)
	ds(plr)
end)

this should work! :slight_smile:


If it works please set it as solution!

Unfortunately, this code does not work for me either. :frowning:

not sure if this matters but in all the data stores i watched it showed it with a - before the value for ex
“-coins”
instead of
“coins”
ill continue looking in my data store i said gat instead of get so ill look for typos

try making 3 seperate data stores for ex
CoinsStore
MegaCoinsStore
CheckiStore

that was 1 of my problems when making my data store also the stores all neda be diferently named im prety sure

Unfortunately, this does not work for me either. :frowning:

W8 when you set mega coins to visible you have it capitalized

Unfortunately, this does not work either. I have tried a lot of things, but unfortunately none of them have worked.