How do i make a loop out of something?

This may be a really dull post,but when i spawn my entity in the game(which is names as “kurya”),he spawns only once,and then,he just will not.As you would see further in the script,you’ll see,that I have already tried to do a loop,but it does not work.
Anyways,here is the script:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local kurya = game:GetService("ReplicatedStorage"):WaitForChild("Kurya"):Clone()
		local hitpart = kurya:WaitForChild("hitpart")
		local approach = kurya:WaitForChild("Approach")
		local air = workspace:WaitForChild("Air")
		local digus = 1

		while wait(digus) do

			wait(10)
			air:Play()
			character:FindFirstChild("WindController").Enabled = true
			task.wait(8)
			print("approaching!")
			character:FindFirstChild("WindController").Enabled = false

			approach:Play()

			digus = math.random(20,35)

			if workspace:FindFirstChild("Assets") then

				kurya.Parent = workspace.Assets
				kurya.CanCollide = false
				kurya.Position = character.HumanoidRootPart.Position + Vector3.new(0,0,0)
				hitpart.Position = character.HumanoidRootPart.Position + Vector3.new(0,0,0)

			end


			while wait(.0000005) do

				for a = 0,20 do
					kurya.Position = kurya.Position + Vector3.new(0,0,math.cos(tick()/10))
					hitpart.Position = hitpart.Position + Vector3.new(0,0,math.cos(tick()/10))
				end 

			end 

		end 

	end)

end)

You used a while loop within another while loop, causing the thread to continuously execute the second while loop without exiting. You can use task.spawn() to prevent the second loop from blocking.

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local kurya = game:GetService("ReplicatedStorage"):WaitForChild("Kurya"):Clone()
		local hitpart = kurya:WaitForChild("hitpart")
		local approach = kurya:WaitForChild("Approach")
		local air = workspace:WaitForChild("Air")
		local digus = 1

		while wait(digus) do

			wait(10)
			air:Play()
			character:FindFirstChild("WindController").Enabled = true
			task.wait(8)
			print("approaching!")
			character:FindFirstChild("WindController").Enabled = false

			approach:Play()

			digus = math.random(20,35)

			if workspace:FindFirstChild("Assets") then

				kurya.Parent = workspace.Assets
				kurya.CanCollide = false
				kurya.Position = character.HumanoidRootPart.Position + Vector3.new(0,0,0)
				hitpart.Position = character.HumanoidRootPart.Position + Vector3.new(0,0,0)

			end

			task.spawn(function()
				while wait(.0000005) do

					for a = 0,20 do
						kurya.Position = kurya.Position + Vector3.new(0,0,math.cos(tick()/10))
						hitpart.Position = hitpart.Position + Vector3.new(0,0,math.cos(tick()/10))
					end 

				end 
			end)

		end 

	end)

end)

Roblox Document For task

1 Like

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