Script error. How do i fix?

Error Message (ServerScriptService.Script:28: attempt to index nil with ‘GetChildren’ - Server
16:36:32.101 Stack Begin - Studio
16:36:32.101 Script ‘ServerScriptService.Script’, Line 28 )

local ToolFolder = game:GetService('ServerStorage'):FindFirstChild("AvatarItems")
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.Character
	local StarterGear = Player.Character

	if ToolData ~= nil then
		for i, v in pairs(ToolData) do
			if ToolFolder:FindFirstChild(v) and Backpack:FindFirstChild(v) == nil  then
				ToolFolder[v]:Clone().Parent = Backpack
			
			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.Character:GetChildren()) do
		table.insert(ToolTable, v.Name)
	end
	if ToolTable ~= nil then
		SaveData:SetAsync(Player.UserId, ToolTable)
	end
end)


1 Like

I think it’s bc the character has already unloaded when the player is leaving, therefore the script is attempting to get children of nil which would cause an error

1 Like

oh. But how do i fix the issue

i got another script and did the same thing

if the player doesn’t lose tools on death, then you should just have it add a folder to a player, and then when the player gets a new tool Player.Backpack.ChildAdded check if the a string value with the tool’s name isn’t in the folder, if it isn’t in the folder then make a new string value in the folder with the tool’s name as its name and when the player leaves replace for i, v in pairs(Player.Character:GetChildren()) with for i, v in pairs(Player.Folder Name:GetChildren())

am i able to save the players avatar items from the character and move it to the folder ?

do you mean accessories or stuff like that?

yeah like accessories and shirts / pants.

what kind of system are you trying to make?

im just trying to save the players acessories so when they rejoin back in they spawn with them. Im using a tool saving script to save the players avatar items

are you trying to make something like an rp game?

its avatar items from a avatar item shop

im trying to make a old roblox sim and im have a avatar shop for it im just trying to save the items

ok, i think what could work is that you still have a folder in the player, and every time the player adds an accessory it adds a string value with the name, and every time the player removes an accessory it removes a string value with the name.

but when the player rejoins how dose it add the items back into the character

so the accessories are in a folder in replicated server of something right?

yeah they are in server storage

is there a way to like when the player is leaving it moves the avatar items to a folder in rs and when the player joins back in it transfer’s it back into the character

ok so you save the folder by using the system you already have in place but with whatever the folder name is, and then when the player joins, you replace the
ToolFolder[v]:Clone.Parent = Backpack withToolFolder[v]:Clone.Parent = Player.Character The Tooldata thing should be in Player.CharacterAdded

no you cannot, if a change is made to replicated storage it wont save when the player joins a different server