How can I add datastore to this script?

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"

local Strength = Instance.new("NumberValue", leaderstats)
Strength.Name = "Strength"
Strength.Value = 1

local Exp = Instance.new("NumberValue", leaderstats)
Exp.Name = "Exp"
Exp.Value = 0

local RequiredExp = Instance.new("NumberValue", player)
RequiredExp.Name = "RequiredExp"
RequiredExp.Value = Strength.Value * 100

Exp.Changed:Connect(function(Changed)
	if Exp.Value >= RequiredExp.Value then
		Exp.Value = 0
		
		Strength.Value += 1
		RequiredExp.Value = Strength.Value * 100
	end
end)

end)

Just add game.players.playerremoving with SetAsync.

can u copy my script and show how to add it please

Just save the data when the player leaves, and get it when they join. If you’d like to know how, read about it here!

can u copy my script and add it into my script please

I’m afraid you’ll have to do it yourself! If you want scripts, use the asset library, not the devforum.

there is just one line to write

Your script is messy and you can’t ask the forum for full code, but ok.

game.Players.PlayerAdder:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"

local Strength = Instance.new("NumberValue", leaderstats)
Strength.Name = "Strength"
Strength.Value = 1

local Exp = Instance.new("NumberValue", leaderstats)
Exp.Name = "Exp"
Exp.Value = 0

local RequiredExp = Instance.new("NumberValue", player)
RequiredExp.Name = "RequiredExp"
RequiredExp.Value = Strength.Value * 100

Exp.Changed:Connect(function(Changed)
	if Exp.Value >= RequiredExp.Value then
		Exp.Value = 0
		
		Strength.Value += 1
		RequiredExp.Value = Strength.Value * 100
	end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success,err = pcall(function()
game:GetService("DataStoreService"):SetAsync(player.UserId,{player.leaderstats.Strength.Value,player.leaderstats.Exp.Value,player.RequiredExp.Value}
end)
if err then
-- do something idk
end
end)

This does not provide loading, though

uhm- No? You need to save it when the player leaves and retrieve it when you leave… That’s a fair amount of code-

Before you copy and paste my script, let me give advice and info to the thread

  • Don’t use parent as an argument for Instance.new() because it takes much longer

  • Use :BindToClose instead of PlayerRemoving or else the Data won’t save if a player loses connection or if the game shuts down. PlayerRemoving only works when a player leaves the game intentionally.

@regexman weren’t you the one who told me to use BindToClose instead of PlayerRemoving in the first place?

local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreSerivce"):GetDataStore("DataStoreValues")
local RunService = game:GetService("RunService")

game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"

local Strength = Instance.new("IntValue")
Strength.Parent = leaderstats
Strength.Name = "Strength"
Strength.Value = 1

local Exp = Instance.new("NumberValue")
Exp.Parent = leaderstats
Exp.Name = "Exp"
Exp.Value = 0

local RequiredExp = Instance.new("NumberValue")
local
RequiredExp.Name = "RequiredExp"
RequiredExp.Value = Strength.Value * 100

Exp.Changed:Connect(function(Changed)
	if Exp.Value >= RequiredExp.Value then
		Exp.Value = 0
		
		Strength.Value += 1
		RequiredExp.Value = Strength.Value * 100
	end
end)

local value1Data = Strength
local value2Data = Exp

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId.."-Value1") or 0 --check if they have data, if not it'll be "0"
        value2Data = DataStore:GetAsync(player.UserId.."Value1") or 0
	end)

	if s then
		Strength.Value = value1Data --setting data if its success
        Exp.Value = value2Data
	else
		game:GetService("TestService"):Error(e)  --if not success then we error it to the console
	end
end)

game:BindToClose(function(player)
if not RunService:IsStudio()
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId.."-Value1", player.leaderstats.Strength.Value) --setting data
    DataStore:SetAsync(player.UserId.."-Value2", player.leaderstats.Exp.Value)
	end)
	    if not s then game:GetService("TestService"):Error(e) 
	end

    end)
end)

Not instead, but you should use both.