How can i make a datastore for this?

Can’t you just copy and paste the script so I can show you? lol

Use 3 back ticks ``` to make a code block

-- this is a code block

is essentially:
```
– this is a code block
```

game.Players.PlayerAdded:Connect(function(Plr)
local stats = Instance.new(“Folder”, Plr)
stats.Name = “Data”
— Level System
local Levels = Instance.new(“IntValue”, stats)
Levels.Name = “Levels”
Levels.Value = 1
local Exp = Instance.new(“IntValue”, stats)
Exp.Name = “Exp”
Exp.Value = 0
local ExpNeed = Instance.new(“IntValue”, stats)
ExpNeed.Name = “ExpNeed”
ExpNeed.Value = 200
— Money System
local Gold = Instance.new(“IntValue”, stats)
Gold.Name = “Gold”
Gold.Value = 1000000000000
— Stats Text
local DefenseP = Instance.new(“IntValue”, stats)
DefenseP.Name = “DefenseP”
DefenseP.Value = 1
local SwordP = Instance.new(“IntValue”, stats)
SwordP.Name = “SwordP”
SwordP.Value = 1
local LuckP = Instance.new(“IntValue”, stats)
LuckP.Name = “LuckP”
LuckP.Value = 1
local DevilFruit = Instance.new(“IntValue”, stats)
DevilFruit.Name = “DevilFruit”
DevilFruit.Value = 1
— Stats System
local Points = Instance.new(“IntValue”, stats)
Points.Name = “Points”
Points.Value = 1000000
local PointsS = Instance.new(“IntValue”, stats)
PointsS.Name = “PointsS”
PointsS.Value = 1
local Defense = Instance.new(“IntValue”, stats)
Defense.Name = “Defense”
Defense.Value = 0
local Sword = Instance.new(“IntValue”, stats)
Sword.Name = “Sword”
Sword.Value = 0
local Luck = Instance.new(“IntValue”, stats)
Luck.Name = “Luck”
Luck.Value = 0
local Special = Instance.new(“IntValue”, stats)
Special.Name = “Special”
Special.Value = 0
end)

game.Players.PlayerAdded:Connect(function(plr)
wait(.1)
local Exp = plr.Data.Exp
local Levels = plr.Data.Levels
local ExpNeed = plr.Data.ExpNeed
local Points = plr.Data.Points

while wait() do
	if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
		Levels.Value = Levels.Value + 1
		Points.Value = Points.Value + 3
		Exp.Value = Exp.Value - ExpNeed.Value
		ExpNeed.Value = ExpNeed.Value + 100
		game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
	end
end

end)

coroutine.wrap(function()
local dss = game:GetService(“DataStoreService”)
local dataStore = dss:GetDataStore(“StatsData”)
local players = game:GetService(“Players”)

local function SerializeLeaderstats(player) : {[string]: number}
local stats = {}

-- this loops through the leaderstats folder and sets the correct values in the table
for _, v in pairs(player.leaderstats:GetChildren()) do
	stats[v.Name] = v.Value
end

return stats

end

local function LoadLeaderstatsData(player)
– “pcall” stands for protected call which basically helps with errors that may possibly occur
local success, err = pcall(function()
– this returns the player’s data
return dataStore:GetAsync(player.UserId)
end)

if success then
	-- this checks if the call was successful
	-- this also checks if they are new/have any data

	if err ~= nil then
		for name, value in pairs(err) do
			-- this loops through the serialized table and sets the values accordingly
			player.leaderstats[name].Value = value
		end
	end
end

return err

end

local function OnPlayerExit(player)
– does what the other one does except it saves the serialized data
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, SerializeLeaderstats(player))
end)

if not success then
	-- if it couldn't save, it will send a warning in the output telling you why
	warn("unable to save data:", err)
end

end

players.PlayerAdded:Connect(function(player)
– do stuff (place the default things in here)
LoadLeaderstatsData(player) – this calls the loading function so the player’s data can load
end)

players.PlayerRemoving:Connect(OnPlayerExit) – this detects when the player leaves
end)()

local dss = game:GetService("DataStoreService")
local dataStore = dss:GetDataStore("StatsData")
local players = game:GetService("Players")

local function SerializeLeaderstats(player) : {[string]: number}
	local stats = {}

	-- this loops through the leaderstats folder and sets the correct values in the table
	for _, v in pairs(player.Data:GetChildren()) do
		stats[v.Name] = v.Value
	end

	return stats
end

local function LoadLeaderstatsData(player)
	-- "pcall" stands for protected call which basically helps with errors that may possibly occur
	local success, err = pcall(function()
		-- this returns the player’s data
		return dataStore:GetAsync(player.UserId)
	end)

	if success then
		-- this checks if the call was successful
		-- this also checks if they are new/have any data

		if err ~= nil then
			for name, value in pairs(err) do
				-- this loops through the serialized table and sets the values accordingly
				player.Data[name].Value = value
			end
		end
	end

	return err
end

local function OnPlayerExit(player)
	-- does what the other one does except it saves the serialized data
	local success, err = pcall(function()
		dataStore:SetAsync(player.UserId, SerializeLeaderstats(player))
	end)

	if not success then
		-- if it couldn't save, it will send a warning in the output telling you why
		warn("unable to save data:", err)
	end
end

players.PlayerAdded:Connect(function(plr)
	local stats = Instance.new("Folder", plr)
	stats.Name = "Data"
	-- Level System

	local Levels = Instance.new("IntValue", stats)
	Levels.Name = "Levels"
	Levels.Value = 1

	local Exp = Instance.new("IntValue", stats)
	Exp.Name = "Exp"
	Exp.Value = 0

	local ExpNeed = Instance.new("IntValue", stats)
	ExpNeed.Name = "ExpNeed"
	ExpNeed.Value = 200
	-- Money System

	local Gold = Instance.new("IntValue", stats)
	Gold.Name = "Gold"
	Gold.Value = 1000000000000
	-- Stats Text

	local DefenseP = Instance.new("IntValue", stats)
	DefenseP.Name = "DefenseP"
	DefenseP.Value = 1

	local SwordP = Instance.new("IntValue", stats)
	SwordP.Name = "SwordP"
	SwordP.Value = 1

	local LuckP = Instance.new("IntValue", stats)
	LuckP.Name = "LuckP"
	LuckP.Value = 1

	local DevilFruit = Instance.new("IntValue", stats)
	DevilFruit.Name = "DevilFruit"
	DevilFruit.Value = 1

	-- Stats System
	local Points = Instance.new("IntValue", stats)
	Points.Name = "Points"
	Points.Value = 1000000

	local PointsS = Instance.new("IntValue", stats)
	PointsS.Name = "PointsS"
	PointsS.Value = 1

	local Defense = Instance.new("IntValue", stats)
	Defense.Name = "Defense"
	Defense.Value = 0

	local Sword = Instance.new("IntValue", stats)
	Sword.Name = "Sword"
	Sword.Value = 0

	local Luck = Instance.new("IntValue", stats)
	Luck.Name = "Luck"
	Luck.Value = 0

	local Special = Instance.new("IntValue", stats)
	Special.Name = "Special"
	Special.Value = 0

	coroutine.wrap(function()
		while wait() do
			if Exp.Value >= (100 * (Levels.Value + 1)) and Levels.Value <= 399 then
				Levels.Value = Levels.Value + 1
				Points.Value = Points.Value + 3
				Exp.Value = Exp.Value - ExpNeed.Value
				ExpNeed.Value = ExpNeed.Value + 100
				game.ReplicatedStorage.LevelSystem.LevelUpGui:FireClient(plr)
			end
		end
	end)()

	LoadLeaderstatsData(plr) -- this calls the loading function so the player’s data can load
end)

players.PlayerRemoving:Connect(OnPlayerExit) -- this detects when the player leaves

Yo omg it worked but how can i make it save my health??

When i put stat points into my Defense it gives me more health but thats the only thing that doesnt save

When the player joins the game and their defense stats load in, just use whatever you use to calculate their health

I dont understand what your trying to say

This is what i use to give the player more health

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(character)
wait()
local Data = player.Data
local MaxHealth = Data:WaitForChild(“MaxHealth”)
character.Humanoid.MaxHealth = MaxHealth.Value + 100
character.Humanoid.Health = MaxHealth.Value + 100
end)
end)

1 Like

Like, when a player upgrades their defense stat and their health increases then they leave, just use the system to upgrade their health when they join the game

Game --> LoadsPlayer --> Upgrades Defense --> Upgrades Health --> Leaves
Game --> LoadsPlayer --> Load their data --> Check what their defense stat is at --> Upgrade Health

Compact it all into a table, encode it and save it to a datastore