-- Variables
local DataStore = game:GetService("DataStoreService"):GetDataStore("DSFE##%@$@#DZX")
local DataKey = ")9118@7Jf2@"

-- Functions
function saveData(Player,DataTable)
	local Playerkey = DataKey..Player.userId
	DataTable:SetAsync(Playerkey,DataTable)
end

function loadData(Player,Stat,Num)
	local Playerkey = DataKey..Player.userId
	local PlayerData = DataStore:GetAsync(Playerkey)
	if PlayerData then
		Stat.Value = PlayerData[Num]
	end
end

function PlayerJoined(Player)
	local Stats = Instance.new("Folder")
	Stats.Name = (Player.userId*3)/5^2 
	local Level = Instance.new("NumberValue")
	Level.Name = "Level"
	Level.Value = 1
	local Xp = Instance.new("NumberValue")
	Xp.Name = "XP"
	Xp.Value = 0
	local Emeralds = Instance.new("NumberValue")
	Emeralds.Name = "Emeralds"
	Emeralds.Value = 1000
	Level.Parent = Stats
	Xp.Parent = Stats
	Emeralds.Parent = Stats
	Stats.Parent = game.ReplicatedStorage.Something
	
	-- Leaderstats
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	local L = Instance.new("NumberValue")
	L.Name = "Level"
	local X = Instance.new("NumberValue")
	X.Name = "XP"
	local E = Instance.new("NumberValue")
	E.Name = "Emeralds"
	L.Parent = ls
	X.Parent = ls
	E.Parent = ls
	ls.Parent = Player
	
	loadData(Player,Level,1)
	loadData(Player,Xp,2)
	loadData(Player,Emeralds,3)
	
	L.Value = Level.Value
	X.Value = Xp.Value
	E.Value = Emeralds.Value
	
	Level.Changed:Connect(function(val)
		L.Value = val
	end)
	Xp.Changed:Connect(function(val)
		X.Value = val
	end)
	Emeralds.Changed:Connect(function(val)
		E.Value = val
	end)
end

game:BindToClose(function(Player)
	local Stats = game.ReplicatedStorage.Something[(Player.userId*3)/5^2]
	local Level = Stats.Level
	local Xp = Stats.XP
	local Emeralds = Stats.Emeralds
	local DataTable = {Level.Value,Xp.Value,Emeralds.Value}
	saveData(Player,DataTable)
end)

game.Players.PlayerAdded:Connect(PlayerJoined)

2 comments

0

You are not saving player data when they leave.

0

Yea i just noticed i did DataTable:SetAsync not DataStore:SetAsync lol