I've had a lot of problems with DataStores, I don't know why but I don't understand how the work.

I've gotten rid of a bunch of mechanics and functions in my game because I don't understand how to save Data. I can figure out how to save some data. Like kills, but I don't understand how to save multiple, I've tried DataStore2's but I'm not sure how to save the data.

I'd be really grateful if someone could please explain!

leaderstats-

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


game.Players.PlayerAdded:Connect(function(plr)
	
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = plr
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"	
	kills.Parent = stats
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	deaths.Parent = stats	
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	gems.Parent = stats	
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	coins.Parent = stats	
	
	plr.CharacterAdded:connect(function(char)
		
		local humanoid
		
		repeat
			humanoid = char:FindFirstChild("Humanoid")
			wait()
		until humanoid
		
		humanoid.Died:connect(function()
			
			deaths.Value = deaths.Value + 1
			
			local tag = humanoid:FindFirstChild("creator")
			
			if tag then
				
				local killer = tag.Value
				
				if killer then
					
					killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
				end
			end
		end)
	end)
	
end)

My DataStore2


local DataStore2 = require(game.ServerScriptService:WaitForChild("DataModule")) --Gets DataStore Module
local MainKey = "MainKey" -- The Key
DataStore2.Combine(MainKey,"Stats","Achievment")

--DataTable

local function SetDataTable()-- Setting Variables In A Table
	local UserData = {
		Stats = {
			["Kills"] = 0,
			["Deaths"] = 0,
			["XP"] = 0,
			["XPN"] = 0,
			["Level"] = 1,
			["Coins"] = 0,
			["Gems"] = 0,
			
		},
		
	}
	return UserData
end


game.Players.PlayerAdded:Connect(function(plr) -- Does Function When Player Joins
	local UserData = DataStore2(MainKey,plr):Get(SetDataTable()) --Gets Table
	
	
	local leaderstats = Instance.new("Folder") ---Leaderstats!--
	leaderstats.Name = "leaderstats"
	
	local kills = Instance.new("IntValue")
	kills.Name = "Kills"	
	
	local deaths = Instance.new("IntValue")
	deaths.Name = "Deaths"
	
	local xp = Instance.new("IntValue")
	xp.Name = "Xp"
	xp.Value = 0
	
	local level = Instance.new("IntValue")
	level.Name = "Level"
	
	local xpn = Instance.new("IntValue")
	xpn.Name = "Xpn"
	
	local coins = Instance.new("IntValue")
	coins.Name = "Coins"
	
	local gems = Instance.new("IntValue")
	gems.Name = "Gems"
	
	local StatsData = DataStore2("Stats",plr)
	
	local function UpdateStats(updatedValue) --Updates Data
		kills.Value = StatsData:Get(updatedValue).Kills
		deaths.Value = StatsData:Get(updatedValue).Deaths
		xp.Value = StatsData:Get(updatedValue).XP
		xpn.Value = StatsData:Get(updatedValue).XPN
		level.Value = StatsData:Get(updatedValue).Level
		coins.Value = StatsData:Get(updatedValue).Coins
		gems.Value = StatsData:Get(updatedValue).Gems		
	end
	
	
	
	UpdateStats(UserData.Stats) -- updates data and calls the function :o
		
	StatsData:OnUpdate(UpdateStats) -- calls func on update
	
	--Parenting Properties
	leaderstats.Parent = plr
	kills.Parent = leaderstats
	deaths.Parent = leaderstats
	xp.Parent = leaderstats
	xpn.Parent = leaderstats
	level.Parent = leaderstats
	coins.Parent = leaderstats
	gems.Parent = leaderstats
	
	--Giving Stats
	
	while true do
		wait(200)
		local KeyData = DataStore2(MainKey)
		KeyData:Save()
	end
	
	xpn.Value = level.Value*1000
	
	
	xp.Value = kills * 1000
	
	
	plr.CharacterAdded:connect(function(char)
		
		local humanoid
		
		repeat
			humanoid = char:FindFirstChild("Humanoid")
			wait()
		until humanoid
		
		humanoid.Died:connect(function()
			
			deaths.Value = deaths.Value + 1
			
			
			local tag = humanoid:FindFirstChild("creator")
			
			if tag then
				
				local killer = tag.Value
				
				if killer then
					
					killer.leaderstats.Kills.Value = killer.leaderstats.Kills.Value + 1
					
					
				end
			end
		end)
	end)


	xp.Changed:connect(function()
		if xp.Value >= xpn.Value then
			xp.Value = 0
			
			level.Value = level.Value + 1
			
			xpn.Value = level.Value*1000
			
		end
	end)
	
end)

2 comments

0

I wrote this a while ago. Hope it can help somewhat: https://devforum.roblox.com/t/datastore-system-how-it-works/408452/5

0

Would that work with multiple currencies?