Data Store Issue (UNSOLVED AGAIN)

Ok so I want items to save when I leave the game my script is:

I did use a YouTube tutorial:

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)```

try using GetService(), just in case the game doesn’t load in time

game:GetService("Players").PlayerAdded:Connect(function(Player)

Can you clarify what the issue is here? What exactly isn’t working? Did you try debugging at all? Are there any errors? These questions are essential to us helping you. We can’t really assist you if you don’t tell us what the problem is.

so ive tried looking alot of times and re editing the script but it dont save the items after leaving the game my script is know

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)

Try wrapping it to a pcall and printing whether it is successful or not, and the error message. Do you get any errors? Also, are testing this in-game or in studio. If in studio, do you have the access to API services enabled?

Both the output don’t say anything at all sometimes it works sometimes it dont

You dont set the variable ToolFolder anywhere.

what do you mean im not good at scripting

he literally explained it smh, you never defined what ToolFolder is

At the top of the script add:

local ToolFolder = (path to your tool folder here)

Don’t you see ToolFolder variable as redlined?

Ok done it is it like this

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)

No its not in my script

char limit

Did you solve the problem/error?

Ah yes it does its soooo confusing sometimes it works and then breaks I’m so embarrassed thanks to all that helped

if it works sometimes you might have multiple of the same thing within the same folder when cloning it might be from a different script just run the game and check to see if there are multiple

1 Like

add the

game:BindToClose(function()
   task.wait(2)
end)
1 Like

Ok the issue is the same again I tested this and somewhat is true I looked in the Starter gear and when I buy an item it don’t go in my starter gear the code I’m using is this so it only saves the stuff i buy once only…

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)

Alright I seen something the swords are in the starter gear only 2 though and 3 others are in backpack

explorer