Script doesn't load tools 70% of the time when the player joins

This is only a fraction of the script, but I think this is the place where it is at fault.
The Table is a table with tool names that the server saved when the player left the game, My problem here is that this script doesn’t give the tools most of the time when the player joins but It prints it like it should.

function LoadTool(PlayerName, Table)
	local attempt = 1
	print("Loading", Table, " Into", PlayerName)
	local playerfolder = folder[PlayerName]
	
	repeat task.wait() until game.Workspace:WaitForChild(PlayerName)
	
	repeat
	for _, v in pairs(Table) do
		local tool = game.ServerStorage.Bats[v]:Clone()
		if tool then
			tool.Parent = playerfolder
			tool:Clone().Parent = game.Players[PlayerName].Backpack
			print("gave", PlayerName, tool.Name)
		end
	end
--half of the script
1 Like

I did some optimization that may have helped with the script.

function LoadTool(PlayerName, Table)
	local player = game:GetService('Players'):FindFirstChild(PlayerName)

	local attempt = 1
	print("Loading", Table, " Into", PlayerName)
	local playerfolder = folder[PlayerName]
	
	repeat wait() until player['Character']
	
	repeat
	for _, v in pairs(Table) do
		local tool = game.ServerStorage.Bats:FindFirstChild(v) 
		if tool then
			tool  = tool:Clone()
			tool:Clone().Parent = player:WaitForChild('Backpack')
			tool:Clone().Parent = playerfolder
			print("gave", PlayerName, tool.Name)
		end
	end

if that doesnt work it’s probably a problem somewhere else

1 Like

Thank you for your help, I also found a problem in the datastores I had that loads up this but I fixed it right away.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.