Loop would not work. no errors

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    i want for the tools number/perk id to be loaded but it does not load

  2. What is the issue?
    the loop command didnt work and does not give error. the prints on the loop didnt print. tried fixing for days

  3. What solutions have you tried so far?
    using coroutines and other things

script is on serverscriptservice

code:

local DataStoreService = game:GetService("DataStoreService")
local toolperk = DataStoreService:GetDataStore("Toolperk9")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		--local toolsunsort = ]

			local toolss = game.ReplicatedStorage.Tools:GetChildren()
		local tools = {} 
		if toolperk:GetAsync(player.UserId) ~= nil then
			print(tools)
		end
		table.remove(toolperk:GetAsync(player.UserId),2)
		print(toolperk:GetAsync(player.UserId))
--[[
		if toolperk:GetAsync(player.UserId) then
			--table.insert(tools,1,toolperk:GetAsync(player.UserId)) 
			for i,v in pairs(toolperk:GetAsync(player.UserId)) do
				local count = 0
				
				repeat count += 1 until count >= 100 or table.find(toolperk:GetAsync(player.UserId),toolss[count] ) 
				--for i,v in pairs(toolss) do
				--	count += 1
				print(toolss[count])
				--end
				tools[#tools+1] = toolperk:GetAsync(player.UserId)[v]
				table.insert(tools,count) 
				if toolperk:GetAsync(player.UserId) ~= nil then
					table.remove(tools,2)
					print(tools)
				end
			end
		else
			tools = {}
		end
		--]]
		if toolperk:GetAsync(player.UserId) then
			
			for i,v in pairs(toolperk:GetAsync(player.UserId))  do
				--table.insert(tools,#tools + 1,toolperk:GetAsync(player.UserId)[i]) 
				tools[#tools + 1] = toolperk:GetAsync(player.UserId)[i]
				--toolperk:GetAsync(player.UserId)
			end
		else
			tools = {}
		end
		if toolperk:GetAsync(player.UserId) ~= nil then
			print(tools)
		end
		--repeat wait() until tools
		
		character.ChildAdded:Connect(function(tool)

			if tool:FindFirstChild("perkid") then print(tool.Name)
				tool:FindFirstChild("perkid").Changed:Connect(function()
					if tool:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == tool.Name
						tools[#tools + 1] = count table.sort(tools)--table.insert(tools,#tools + 1,tool.Name)
						print(game.ReplicatedStorage.Tools:GetChildren()[count])

						print(tools)

						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end)
		player:WaitForChild("Backpack").ChildAdded:Connect(function(tool)
			print(tool.Name)
			if tool:FindFirstChild("perkid") then
				tool:FindFirstChild("perkid").Changed:Connect(function()
					if tool:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == tool.Name
						tools[#tools + 1] = count table.sort(tools)--table.insert(tools,#tools + 1,tool.Name)
						print(game.ReplicatedStorage.Tools:GetChildren()[count]) 

						print(tools)

						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end)
		for i,v in pairs(character:GetChildren()) do
			repeat wait() until v:FindFirstChild("perkid")
				v:FindFirstChild("perkid").Changed:Connect(function()
					if v:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == v.Name
						tools[#tools + 1] = count--table.insert(tools,#tools + 1,tool.Name)
						print(tools)
						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
					end
					print(tools[count])
						print("saved")
					end
				end)

		end
		
		for i,v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
			if v:FindFirstChild("perkid") then
				v:FindFirstChild("perkid").Changed:Connect(function()
					if v:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == v.Name
						tools[#tools + 1] = count--table.insert(tools,#tools + 1,tool.Name)
						print(tools)
						--table.insert(tools)
						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end

		while wait() do ------ Problem ---------~~~~~~~~
			-- This function might do some work for a bit then yield some value
			repeat
			print("0")
			for i,v in pairs(tools) do 
				print("1")
				if toolss[v] ~= nil then
					print("2")
					local name = toolss[v].Name
					print(name)
					--print(toolss[v].Name)

					if player.Backpack:FindFirstChild(name) then
						print("3")
						player.Backpack:FindFirstChild(name):WaitForChild("perkid").Value = 1
						--player.Backpack:FindFirstChild(name).Name = name.." {upgraded}"
						--print(tools)
					end
				end
			end
			wait()

				until script == nil
		end


		--[[
		for i,v in pairs(toolss) do
			if toolperk:GetAsync(player.UserId) and toolperk:GetAsync(player.UserId) ~= nil then
				if table.find(tools,v.Name)then
					v:WaitForChild("perkid").Value = 1
				end
			end
		end
		--]]
	end)
	
	
end)


you may not understand by my messy scripting, take your time. any help is appreciated

what part of the code exactly isnt running? also have you checked what “Toolperk9” is returning

1 Like

it returns numbers like tool id from the tool folder in replicated storage that given to players. and if the player’s tool’s id is in the toolperk9, then the tool’s perk id value will be increased

the while wait() do loop has prints like print(0) in it but the print is not working

what part in specific isnt running?

1 Like

the while wait() do loop is not working

there isnt a coroutine.resume so the function just stops and never gets resumed.

1 Like

i forgot to mention that i changed it to while true do loop

that doesnt change much. you still need to resume the coroutine.

1 Like

i added

		local function task()
			-- This function might do some work for a bit then yield some value
			repeat
				print("0")
				for i,v in pairs(tools) do 
					print("1")
					if toolss[v] ~= nil then
						print("2")
						local name = toolss[v].Name
						print(name)
						--print(toolss[v].Name)

						if player.Backpack:FindFirstChild(name) then
							print("3")
							player.Backpack:FindFirstChild(name):WaitForChild("perkid").Value = 1
							--player.Backpack:FindFirstChild(name).Name = name.." {upgraded}"
							--print(tools)
						end
					end
				end
				wait()
				coroutine.yield("first")  -- To be returned by coroutine.resume()
				-- The function continues once it is resumed again 
			until script == nil
			return "second"
		end

		local taskCoro = coroutine.create(task)
		-- Call resume for the first time, which runs the function from the beginning
		local success, result = coroutine.resume(taskCoro)

to the script:

local DataStoreService = game:GetService("DataStoreService")
local toolperk = DataStoreService:GetDataStore("Toolperk9")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		--local toolsunsort = ]

			local toolss = game.ReplicatedStorage.Tools:GetChildren()
		local tools = {} 
		if toolperk:GetAsync(player.UserId) ~= nil then
			print(tools)
		end
		table.remove(toolperk:GetAsync(player.UserId),2)
		print(toolperk:GetAsync(player.UserId))
--[[
		if toolperk:GetAsync(player.UserId) then
			--table.insert(tools,1,toolperk:GetAsync(player.UserId)) 
			for i,v in pairs(toolperk:GetAsync(player.UserId)) do
				local count = 0
				
				repeat count += 1 until count >= 100 or table.find(toolperk:GetAsync(player.UserId),toolss[count] ) 
				--for i,v in pairs(toolss) do
				--	count += 1
				print(toolss[count])
				--end
				tools[#tools+1] = toolperk:GetAsync(player.UserId)[v]
				table.insert(tools,count) 
				if toolperk:GetAsync(player.UserId) ~= nil then
					table.remove(tools,2)
					print(tools)
				end
			end
		else
			tools = {}
		end
		--]]
		if toolperk:GetAsync(player.UserId) then
			
			for i,v in pairs(toolperk:GetAsync(player.UserId))  do
				--table.insert(tools,#tools + 1,toolperk:GetAsync(player.UserId)[i]) 
				tools[#tools + 1] = toolperk:GetAsync(player.UserId)[i]
				--toolperk:GetAsync(player.UserId)
			end
		else
			tools = {}
		end
		if toolperk:GetAsync(player.UserId) ~= nil then
			print(tools)
		end
		--repeat wait() until tools
		
		character.ChildAdded:Connect(function(tool)

			if tool:FindFirstChild("perkid") then print(tool.Name)
				tool:FindFirstChild("perkid").Changed:Connect(function()
					if tool:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == tool.Name
						tools[#tools + 1] = count table.sort(tools)--table.insert(tools,#tools + 1,tool.Name)
						print(game.ReplicatedStorage.Tools:GetChildren()[count])

						print(tools)

						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end)
		player:WaitForChild("Backpack").ChildAdded:Connect(function(tool)
			print(tool.Name)
			if tool:FindFirstChild("perkid") then
				tool:FindFirstChild("perkid").Changed:Connect(function()
					if tool:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == tool.Name
						tools[#tools + 1] = count table.sort(tools)--table.insert(tools,#tools + 1,tool.Name)
						print(game.ReplicatedStorage.Tools:GetChildren()[count]) 

						print(tools)

						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end)
		for i,v in pairs(character:GetChildren()) do
			repeat wait() until v:FindFirstChild("perkid")
				v:FindFirstChild("perkid").Changed:Connect(function()
					if v:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == v.Name
						tools[#tools + 1] = count--table.insert(tools,#tools + 1,tool.Name)
						print(tools)
						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
					end
					print(tools[count])
						print("saved")
					end
				end)

		end
		
		for i,v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
			if v:FindFirstChild("perkid") then
				v:FindFirstChild("perkid").Changed:Connect(function()
					if v:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == v.Name
						tools[#tools + 1] = count--table.insert(tools,#tools + 1,tool.Name)
						print(tools)
						--table.insert(tools)
						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end

		local function task()---Added`````~~~~~~~
			-- This function might do some work for a bit then yield some value
			repeat
				print("0")
				for i,v in pairs(tools) do 
					print("1")
					if toolss[v] ~= nil then
						print("2")
						local name = toolss[v].Name
						print(name)
						--print(toolss[v].Name)

						if player.Backpack:FindFirstChild(name) then
							print("3")
							player.Backpack:FindFirstChild(name):WaitForChild("perkid").Value = 1
							--player.Backpack:FindFirstChild(name).Name = name.." {upgraded}"
							--print(tools)
						end
					end
				end
				wait()
				coroutine.yield("first")  -- To be returned by coroutine.resume()
				-- The function continues once it is resumed again 
			until script == nil
			return "second"
		end

		local taskCoro = coroutine.create(task)
		-- Call resume for the first time, which runs the function from the beginning
		local success, result = coroutine.resume(taskCoro)---and the resume`````~~~~~~~


		--[[
		for i,v in pairs(toolss) do
			if toolperk:GetAsync(player.UserId) and toolperk:GetAsync(player.UserId) ~= nil then
				if table.find(tools,v.Name)then
					v:WaitForChild("perkid").Value = 1
				end
			end
		end
		--]]
	end)
	
	
end)


still not working though. no errors. everything else in the script works except the coroutine. did i do something wrong?

what are you trying to do with this loop?

1 Like

i want to make it so it can increase the tool’s perk id

have you tried checking if the WaitForChild is inf yeilding

1 Like

never mind. i fixed it by using functions
thanks for the help anyways

new code:

local DataStoreService = game:GetService("DataStoreService")
local toolperk = DataStoreService:GetDataStore("Toolperk9")
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		--local toolsunsort = ]

			local toolss = game.ReplicatedStorage.Tools:GetChildren()
		local tools = {} 
		if toolperk:GetAsync(player.UserId) ~= nil then
			print(tools)
		end
		table.remove(toolperk:GetAsync(player.UserId),2)
		print(toolperk:GetAsync(player.UserId))
--[[
		if toolperk:GetAsync(player.UserId) then
			--table.insert(tools,1,toolperk:GetAsync(player.UserId)) 
			for i,v in pairs(toolperk:GetAsync(player.UserId)) do
				local count = 0
				
				repeat count += 1 until count >= 100 or table.find(toolperk:GetAsync(player.UserId),toolss[count] ) 
				--for i,v in pairs(toolss) do
				--	count += 1
				print(toolss[count])
				--end
				tools[#tools+1] = toolperk:GetAsync(player.UserId)[v]
				table.insert(tools,count) 
				if toolperk:GetAsync(player.UserId) ~= nil then
					table.remove(tools,2)
					print(tools)
				end
			end
		else
			tools = {}
		end
		--]]
		
		--local taskCoro = coroutine.wrap(function()---Added`````~~~~~~~
			-- This function might do some work for a bit then yield some value
			--while wait() do
			--print("0")
			local function test()
				print("01")
				for i,v in pairs(tools) do 
					print("1")
					if toolss[v] ~= nil then
						print("2")
						local name = toolss[v].Name
						print(name)
						--print(toolss[v].Name)

						if player.Backpack:FindFirstChild(name) then
							print("3")
							player.Backpack:FindFirstChild(name):WaitForChild("perkid").Value = 1
							--player.Backpack:FindFirstChild(name).Name = name.." {upgraded}"
							--print(tools)
						end
					end
				end
			end
		test()
				--coroutine.yield("first")  -- To be returned by coroutine.resume()
				-- The function continues once it is resumed again 
				--coroutine.yield()
				--coroutine.wrap()
			--end
			--return "second"
		--end)
		--taskCoro()
		--coroutine.resume(taskCoro)
		
		-- Call resume for the first time, which runs the function from the beginning
		--local success, result = coroutine.resume(taskCoro)---and the resume`````~~~~~~~
		--success, result = coroutine.resume(taskCoro)

		--print(success, result)
		if toolperk:GetAsync(player.UserId) then
			
			for i,v in pairs(toolperk:GetAsync(player.UserId))  do
				--table.insert(tools,#tools + 1,toolperk:GetAsync(player.UserId)[i]) 
				tools[#tools + 1] = toolperk:GetAsync(player.UserId)[i]
				--toolperk:GetAsync(player.UserId)
			end
		else
			tools = {}
		end
		if toolperk:GetAsync(player.UserId) ~= nil then
			print(tools)
		end
		--repeat wait() until tools
		
		character.ChildAdded:Connect(function(tool)

			if tool:FindFirstChild("perkid") then print(tool.Name) test()
				tool:FindFirstChild("perkid").Changed:Connect(function()
					if tool:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == tool.Name
						tools[#tools + 1] = count table.sort(tools)--table.insert(tools,#tools + 1,tool.Name)
						print(game.ReplicatedStorage.Tools:GetChildren()[count])

						print(tools)
						--success, result = coroutine.resume(taskCoro)
						--print(success, result)
						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end)
		--success, result = coroutine.resume(taskCoro)
		--print(success, result)
		player:WaitForChild("Backpack").ChildAdded:Connect(function(tool)
			print(tool.Name)
			test()
			if tool:FindFirstChild("perkid") then
				tool:FindFirstChild("perkid").Changed:Connect(function()
					if tool:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == tool.Name
						tools[#tools + 1] = count table.sort(tools)--table.insert(tools,#tools + 1,tool.Name)
						print(game.ReplicatedStorage.Tools:GetChildren()[count]) 
						--success, result = coroutine.resume(taskCoro)
						--print(success, result)
						print(tools)

						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
					end
				end)
			end
		end)
		--success, result = coroutine.resume(taskCoro)
		--print(success, result)
		for i,v in pairs(character:GetChildren()) do
			repeat wait() until v:FindFirstChild("perkid")
				v:FindFirstChild("perkid").Changed:Connect(function()
					if v:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == v.Name
						tools[#tools + 1] = count--table.insert(tools,#tools + 1,tool.Name)
						print(tools)
						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
					end
					print(tools[count])
					print("saved")
					--success, result = coroutine.resume(taskCoro)
					--print(success, result)
					end
				end)

		end
		--success, result = coroutine.resume(taskCoro)
		--print(success, result)
		for i,v in pairs(player:WaitForChild("Backpack"):GetChildren()) do
			if v:FindFirstChild("perkid") then
				v:FindFirstChild("perkid").Changed:Connect(function()
					if v:FindFirstChild("perkid").Value == 1 then
						local count = 0
						repeat count += 1 until toolss[count].Name == v.Name
						tools[#tools + 1] = count--table.insert(tools,#tools + 1,tool.Name)
						print(tools)
						--table.insert(tools)
						--toolperk:SetAsync(player.UserId,tool.Name)
						for i,v in pairs(tools) do
							toolperk:SetAsync(player.UserId,tools)
						end
						print("saved")
						--success, result = coroutine.resume(taskCoro)
						--print(success, result)
					end
				end)
			end
		end
		--success, result = coroutine.resume(taskCoro)
		--print(success, result)


		--[[
		for i,v in pairs(toolss) do
			if toolperk:GetAsync(player.UserId) and toolperk:GetAsync(player.UserId) ~= nil then
				if table.find(tools,v.Name)then
					v:WaitForChild("perkid").Value = 1
				end
			end
		end
		--]]
	end)
	
	
end)



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