How can i make a datastore for this script?

because i like it seperate so it seasier to modify a certain part of the script

1 Like

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

local items = Instance.new("Folder")

plr.CharacterAdded:Connect(function(char)

	local hum = char:FindFirstChild("Humanoid")
	for i, v in pairs(items:GetChildren()) do
		v.Parent = plr.Backpack
	end
	hum.HealthChanged:Connect(function()

		if hum.Health <= 0 then
			if char:FindFirstChildOfClass("Tool") then
				char:FindFirstChildOfClass("Tool").Parent = items
			end
			for i, v in pairs(plr.Backpack:GetChildren()) do
				v.Parent = items
			end
		end

	end)

end)

end)

1 Like

No idea why this triggers when health changes and is less than or equal to 0 humanoid.died is a thing

1 Like

If you are wanting this. Then I would recommend changing your set up quite a lot. This is the table you would want for that.

local TableToSave = { 
["Tools"] = {"Tool1", "Tool2"},
["Leaderstats"] = {["Coins"] = 100, ["AnotherStat"] = false},
}

This above set up is very different then what you have, but its much nicer and cleaner^

1 Like

the tools dont matter ignore tools i already have data store for everything like tools etc. i need leaderstats to carry across servers

1 Like

hm? Your words aren’t matching up. You want 2 datastores?

1 Like

i dont mind weather its all or just coins

1 Like

Would not recommend using two data stores when you can just combine them, even if one is for something and one is for something else.

1 Like

all leaderstats as levels and coins

1 Like

Is tools in your leaderstats……?

1 Like

ok so say i had this data store what script would i need and how would i combine them
local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)

game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)

local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

if ToolData ~= nil then
	for i, v in pairs(ToolData) do
		if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
			ToolFolder[v]:Clone().Parent = Backpack
			ToolFolder[v]:Clone().Parent = StarterGear
		end
	end
end

Player.CharacterRemoving:Connect(function(Character)
	Character:WaitForChild("Humanoid"):UnequipTools()
end)

end)

game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}

for i, v in pairs(Player.Backpack:GetChildren()) do
	table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
	SaveData:SetAsync(Player.UserId, ToolTable)
end

end)

1 Like

no they are in a folder in server storage using a shop system

1 Like

Okay well In short when saving someone’s data your going to want to save it as a dictionary, or in this form:

{[“Tools”] = tool stuff etc, [“Coins”] = Player.leaderstats.Coins.Value}

i get that but what is a script i can attatch to the one for tools i currently have but for coins and how would it look combined

For this where it says

SaveData:SetAsync(Player.UserId, ToolTable)

Replace it with

SaveData:SetAsync(Player.UserId, {[“Tools”] = ToolTable, [“Coins”] = Player.leaderstats.Coins.Value})

You will have to change the “” to be correct because they are different from me being on mobile

what line do you see this on??

for i, v in pairs(Player.Backpack:GetChildren()) do
table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
SaveData:SetAsync(Player.UserId, ToolTable)
end

what do you mean iv in pairs can you edit that into the script so i can see

local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)

game.Players.PlayerAdded:Connect(function(Player)
local ToolData = SaveData:GetAsync(Player.UserId)


local Backpack = Player:WaitForChild("Backpack")
local StarterGear = Player:WaitForChild("StarterGear")

if ToolData ~= nil then
	for i, v in pairs(ToolData) do
		if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
			ToolFolder[v]:Clone().Parent = Backpack
			ToolFolder[v]:Clone().Parent = StarterGear
		end
	end
end

Player.CharacterRemoving:Connect(function(Character)
	Character:WaitForChild("Humanoid"):UnequipTools()
end)

end)

game.Players.PlayerRemoving:Connect(function(Player)
local ToolTable = {}

for i, v in pairs(Player.Backpack:GetChildren()) do
	table.insert(ToolTable, v.Name)
end
if ToolTable ~= nil then
	SaveData:SetAsync(Player.UserId, {[“Tools”] = ToolTable, [“Coins”] = Player.leaderstats.Coins.Value})
end
end)

You can try this script. I haven’t tested it out, but it should save everything. It does make the leaderstats values and folders meaning disable your old script that did that for you.

local ToolFolder = game:GetService(“ServerStorage”):FindFirstChild(“SavedTools”)
local DataStoreService = game:GetService(“DataStoreService”)
local SaveData = DataStoreService:GetDataStore(“SaveData”)

local defaultTable = {
	["Coins"] = 0, --Add your leaderstats here if more then one. The value in here is the default value for new players
	
}

game.Players.PlayerAdded:Connect(function(Player)
	local ToolData = SaveData:GetAsync(Player.UserId)

	local Backpack = Player:WaitForChild("Backpack")
	local StarterGear = Player:WaitForChild("StarterGear")

	if ToolData ~= nil and ToolData["Tools"] ~= nil then
		for i, v in pairs(ToolData["Tools"]) do
			if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
				ToolFolder[v]:Clone().Parent = Backpack
				ToolFolder[v]:Clone().Parent = StarterGear
			end
		end
	end
	
	local LeaderstatsFolder = Instance.new("Folder", Player)
	LeaderstatsFolder.Name = "leaderstats"
	
	if  ToolData == nil or ToolData["Leaderstats"] == nil then
		for i, v in defaultTable do
			local ClassName = nil
			if typeof(v) == "number" then
				ClassName = "IntValue"
			elseif typeof(v) == "string" then
				ClassName = "StringValue"
			elseif typeof(v) == "boolean" then
				ClassName = "BoolValue"
			end
			
			if ClassName ~= nil then
				local value = Instance.new(ClassName, LeaderstatsFolder)
				value.Name = i
				value.Value = v
			end
		end
	else
		for i, v in ToolData["Leaderstats"] do 
			local ClassName = nil
			if typeof(v) == "number" then
				ClassName = "IntValue"
			elseif typeof(v) == "string" then
				ClassName = "StringValue"
			elseif typeof(v) == "boolean" then
				ClassName = "BoolValue"
			end

			if ClassName ~= nil then
				local value = Instance.new(ClassName, LeaderstatsFolder)
				value.Name = i
				value.Value = v
			end
		end
	end
	
	Player.CharacterRemoving:Connect(function(Character)
		Character:WaitForChild("Humanoid"):UnequipTools()
	end)


end)

game.Players.PlayerRemoving:Connect(function(Player)
	local EveythingTable = {}
	local LeaderStatsTable = {}
	local ToolTable = {}
	
	for i, v in Player:FindFirstChild("leaderstats"):GetChildren() do 
		if v:IsA("ValueBase") then 
			LeaderStatsTable[v.Name] = v.Value
		end
	end

	for i, v in pairs(Player.Backpack:GetChildren()) do
		table.insert(ToolTable, v.Name)
	end
	
	
	if ToolTable ~= nil then
		EveythingTable["Tools"] = ToolTable
	else
		EveythingTable["Tools"] = nil
	end
	
	if LeaderStatsTable ~= nil then
		EveythingTable["Leaderstats"] = LeaderStatsTable
	else
		EveythingTable["Leaderstats"] = nil
	end
	

	SaveData:SetAsync(Player.UserId, EveythingTable)
end)