Data store not saving

Its not reloading items in from last play session

My output says nothing

code:

local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")

game:GetService("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:GetService("Players").PlayerAdded: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

Hi!

First of letā€™s clean up your script. :slight_smile:

-- Make a variable for Players
local Players = game:GetService("Players")
game:GetService("Players").PlayerAdded:Connect(function(Player)
-- Have to be:
Players.PlayerAdded:Connect(function(Player)
if ToolData ~= nil then
-- Have to be:
if ToolData then
if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
-- Have to be:
if ToolFolder:FindFirstChild(v) and not Backpack:FindFirstChild(v) and not StarterGear:FindFirstChild(v) then
Players.PlayerAdded:Connect(function(Player)
-- Have to be:
Players.PlayerRemoving:Connect(function(Player)
if ToolTable ~= nil then
-- Have to be:
if ToolTable ~= {} then

Also I would suggest adding a BindToClose event, that saves players data when the server shuts down.

2 Likes

Its because you donā€™t have PlayerRemoving eventā€¦ both of them are PlayerAdded ._. Am I missing something orā€¦?

1 Like

Yes youā€™re also missing that

if ToolTable ~= nil then

Will always not be nill in the last section, since itā€™s defined as {} :stuck_out_tongue:

1 Like

Thank you I will use your script with the if ToolTable ~= il then will that be on the last row of the script? the output also says:

ServerScriptService.SaveTools:17: Expected ā€˜endā€™ (to close ā€˜thenā€™ at column 29), got

You probably forgot an ā€˜endā€™. Send your current code

2 Likes

Ok im using the code by @ItzBloxyDev

local Players = game:GetService("Players")
game:GetService("Players").PlayerAdded:Connect(function(Player)
	-- Have to be:
	Players.PlayerAdded:Connect(function(Player)
		if ToolData ~= nil then
			-- Have to be:
			if ToolData then
				if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil and StarterGear:FindFirstChild(v) == nil then
					-- Have to be:
					if ToolFolder:FindFirstChild(v) and not Backpack:FindFirstChild(v) and not StarterGear:FindFirstChild(v) then
						Players.PlayerAdded:Connect(function(Player)
							-- Have to be:
							Players.PlayerRemoving:Connect(function(Player)
								if ToolTable ~= nil then
									-- Have to be:
									if ToolTable ~= {} then```

When he said ā€œhave to beā€ he meant to replace it like this

local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local ToolData = SaveData:GetAsync(Player.UserId)
	
	local Backpack = Player:WaitForChild("Backpack")
	local StarterGear = Player:WaitForChild("StarterGear")
	
	if ToolData then
		for i, v in pairs(ToolData) do
			if ToolFolder:FindFirstChild(v) and not Backpack:FindFirstChild(v) and not StarterGear:FindFirstChild(v) 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)

Players.PlayerRemoving:Connect(function(Player)
	local ToolTable
	
	for i,v in pairs(Player.Backpack:GetChildren()) do
        if not ToolTable then ToolTable = {} end
		table.insert(ToolTable, v.Name)
	end
	if ToolTable then
		SaveData:SetAsync(Player.UserId, ToolTable)
	end
end)

Yeah Iā€™m very dumb at coding I am trying to learn

1 Like

Okay, so the output says only 1 thing when reloading the game up after buying an item in the last play session

ServerScriptService.SaveTools:13: attempt to index nil with 'FindFirstChild'

its because ToolFolder was never defined. Im guessing you were following a tutorial and missed a line?

I was following a tutorial on YouTube but all my lines matched up to the tutorials one I checked like 4 times I did have this solved but it broke its self when adding a new feature in

can you send the link of the video to me?

Code maybe different because I had to make a other developer forum post it did work but broke after adding something in and the person that helped me was a bit cocky.

local DataStoreService = game:GetService("DataStoreService")
local SaveData = DataStoreService:GetDataStore("SaveData")
local ToolFolder = game:GetService("ServerStorage"):FindFirstChild("SavedTools")
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(Player)
	local ToolData = SaveData:GetAsync(Player.UserId)
	
	local Backpack = Player:WaitForChild("Backpack")
	local StarterGear = Player:WaitForChild("StarterGear")
	
	if ToolData then
		for i, v in pairs(ToolData) do
			if ToolFolder:FindFirstChild(v) and not Backpack:FindFirstChild(v) and not StarterGear:FindFirstChild(v) 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)

Players.PlayerRemoving:Connect(function(Player)
	local ToolTable
	
	for i,v in pairs(Player.Backpack:GetChildren()) do
        if not ToolTable then ToolTable = {} end
		table.insert(ToolTable, v.Name)
	end
	if ToolTable then
		SaveData:SetAsync(Player.UserId, ToolTable)
	end
end)

This should work

1 Like

Ok this broke my animations in the sword and didnā€™t make the item save either when playing in the real game My output says nothing the animation works in studio though

the animations breaking probably has nothing to do with this script but if you add

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

it will save since your testing by yourself

My code for it to animate this is what seems to have broken it

ā€“Animateā€“

	
	if Debounce == false then
		Debounce = true
		
		local Humanoid = Tool.Parent:WaitForChild("Humanoid")
		local AnimTrack = Humanoid:WaitForChild("Animator"):LoadAnimation(Slash)
		AnimTrack.Priority = Enum.AnimationPriority.Action4 --This broke the saving on the last post
		
		AnimTrack:Play()
		wait(1)
		Debounce = false
	end
end)```

What line do i put this on

[Char limit]