Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Auto Save/Load Script not working?


function saveWeapons(player)
	local bin = player.StarterGear
	local stuff = player.Backpack:GetChildren()
	for i = 1,#stuff do
		local name = stuff[i].Name
		if game.StarterPack:findFirstChild(name)==nil and player.StarterGear:findFirstChild(name)==nil  then
			stuff[i]:Clone().Parent = bin
		end
	end
	local char = player.Character:GetChildren()
	for i = 1,#char do
		if char[i].className == "Tool" then
			local name = char[i].Name
			if game.StarterPack:findFirstChild(name)==nil and player.StarterGear:findFirstChild(name)==nil then
				char[i]:Clone().Parent = bin
			end
		end
	end
end
		
function onRespawned(player)
	local findBin = game.Lighting:findFirstChild(player.Name)
	if findBin~=nil then
		local stuff = findBin:GetChildren()
		for i = 1,#stuff do
			stuff[i]:Clone().Parent = player.Backpack
		end
		findBin:Remove()
	end
		saveWeapons(player)
end

function onPlayerEntered(newPlayer)
	newPlayer.Changed:connect(function (property)
		if (property == "Character") then
			onRespawned(newPlayer)
		end
	end)
	while true do
		if newPlayer.Character~=nil then break end
		wait()
	end
		saveWeapons(newPlayer)
end

game.Players.PlayerAdded:connect(onPlayerEntered)

I Do have errors , (this is not a lcoal script), about 'startergear is not a valid member of player' for some reason. Works in studio . thanks , all help appreciated :))

1 answer

✓ Accepted answer

When a player first joins online, their contents load after PlayerAdded is fired. Use WaitForChild to retrieve StarterGear instead of indexing it directly, because it may not exist yet.

1 comment

0

Ah thanks :)

This discussion is preserved as it was. New answers, comments, and votes are closed.