Touched not Running Inside of for loop

Im making a devil fruit pickup system and im at the point where i want to make it where the player can pick up the fruit and that works but only once it works, it works for the first fruit i pick up but for the second one it dosent? im aware that touched runs over and over but it only runs once in my for loop?
i have tried putting a while loop that runs every second on the for loop but it dosent work.
heres my code:

local remote = game.ReplicatedStorage.DestroyFruit
local DevilFruits = game.Workspace.DevilFruit
local textanim = require(game.ReplicatedStorage.AnimText)
local debris = game:GetService("Debris")
local debounce = false
local module = require(script.Parent.ModuleScript)
local playergui = game.Players.LocalPlayer.PlayerGui.DevilFruitFound


local RunService = game:GetService("RunService")

	




local function CreateDevilGui(fruitmodel)
	print("ok")
	if debounce == true then return end
	debounce = true
	local viewpoint = Vector3.new(0,0,0)

	local ViewPort = Instance.new("ViewportFrame")
	ViewPort.Size = UDim2.new(0.3, 0, 0.4, 0)

	ViewPort.Parent = script.Parent

	ViewPort.BackgroundTransparency = 1

	ViewPort.Position = UDim2.new(0.35, 0,0.32, 0)
	local viewportCamera = Instance.new("Camera")

	viewportCamera.CameraType = Enum.CameraType.Scriptable
	ViewPort.CurrentCamera = viewportCamera

	fruitmodel:FindFirstChild("Part").CFrame = CFrame.new(viewpoint)
	fruitmodel.Parent = ViewPort
	local BLurEffect = Instance.new("BlurEffect")
	BLurEffect.Parent = game.Lighting
	BLurEffect.Size = 15

	game:GetService("RunService").RenderStepped:Connect(function()

		local cframe,size = fruitmodel:GetBoundingBox()

		local max = math.max(size.X, size.Y, size.Z)

		local dis = (max/math.tan(math.rad(viewportCamera.FieldOfView))) * 1.5

		local currentdis = (max/2) + dis

		viewportCamera.CFrame = CFrame.new(viewpoint + Vector3.new(0,0,currentdis))

		local fullRotation = math.pi*2
		local currentRotation = tick()%fullRotation

		viewportCamera.CFrame = CFrame.Angles(0, currentRotation, 0) * CFrame.new(viewpoint + Vector3.new(0,0,currentdis),viewpoint)
	end)
	local TweenService = game:GetService("TweenService")
	--Adding in Rainbow text
	local Text = "Congratulations!"
	local RS = game:GetService("RunService")
	local rainbow = Instance.new("TextLabel")

	rainbow.Parent = script.Parent
	rainbow.BackgroundTransparency = 1
	rainbow.TextScaled = true
	rainbow.Position = UDim2.new(0.350, 0,0.189, 0)
	rainbow.Size = UDim2.new(0, 421,0, 50)
	rainbow.TextColor3 = Color3.fromRGB(255, 255, 255)	




	local Text2 = "You Found A Devil Fruit!!"
	local Bottemtext = Instance.new("TextLabel")
	Bottemtext.TextScaled = true
	Bottemtext.BackgroundTransparency = 1 
	Bottemtext.RichText = true
	Bottemtext.Parent = script.Parent
	Bottemtext.Position = UDim2.new(0.350, 0,0.607, 0)
	Bottemtext.Size = UDim2.new(0, 421,0, 50)
	Bottemtext.Text = Text2
	task.spawn(function()

		textanim.typeWrite(rainbow,Text, 0.01) 
	end)
	task.spawn(function()

		textanim.typeWrite(Bottemtext,Text2, 0.02)
	end)

	local grad = Instance.new("UIGradient")
	grad.Parent = rainbow
	grad.Rotation = 180
	local RS = game:GetService("RunService")
	local counter = 0       
	local w = math.pi / 12  
	local CS = {}          
	local num = 15 			
	local frames = 0		

	task.spawn(function()

		RS.Heartbeat:Connect(function()

			if math.fmod(frames, 2) == 0 then
				for i = 0, num do
					local c = Color3.fromRGB(127 * math.sin(w*i + counter) + 128, 127 * math.sin(w*i + 2 * math.pi/3 + counter) + 128, 127*math.sin(w*i + 4*math.pi/3 + counter) + 128)
					table.insert(CS, i+1, ColorSequenceKeypoint.new(i/num, c))
				end
				grad.Color = ColorSequence.new(CS)
				CS = {}
				counter = counter + math.pi/40
				if (counter >= math.pi * 2) then
					counter = 0
				end
			end
			if frames >= 1000 then
				frames = 0
			end
			frames = frames + 1
			wait(3)
			debounce = false
			fruitmodel.Parent = game.Players.LocalPlayer.Backpack
			remote:FireServer(playergui)
			
			Bottemtext:Destroy()
			rainbow:Destroy()
			ViewPort:Destroy()
			viewportCamera:Destroy()
			BLurEffect:Destroy()
			
print("cheese")
		end)	
	end)
end


local connection 
	



for i,v in pairs(DevilFruits:GetChildren()) do
	v:FindFirstChild("Part").Touched:Connect(function(hit)
		
		if hit then
			print("cjeee")
			
			CreateDevilGui(v)
			print(debounce)
			
		end	
		
		
		
	
end)

end

	
1 Like

If you pick up the 2nd fruit right after the 1st one, does it print “cjeee” immediately or does it wait a few seconds?

2 Likes

Wait a minute, why is wait(3) debounce = false connected to Heartbeat? Wouldn’t that code then run every single heartbeat?

1 Like

No the end Statement is before that line of code

1 Like

When you delete the other stuff you get this

	task.spawn(function()
		RS.Heartbeat:Connect(function()
			wait(3)
			debounce = false
		end)	
	end)
1 Like

wouldn’t spawn just be better instead of task.spawn

Dude, task.spawn() was made to replace spawn(). It’s also better than spawn performance wise.

@BappoSama Try this:

local remote = game.ReplicatedStorage.DestroyFruit
local DevilFruits = game.Workspace.DevilFruit
local textanim = require(game.ReplicatedStorage.AnimText)
local debris = game:GetService("Debris")
local debounce = false
local module = require(script.Parent.ModuleScript)
local playergui = game.Players.LocalPlayer.PlayerGui.DevilFruitFound


local RunService = game:GetService("RunService")






local function CreateDevilGui(fruitmodel)
	print("ok")
	if debounce == true then return end
	debounce = true
	local viewpoint = Vector3.new(0,0,0)

	local ViewPort = Instance.new("ViewportFrame")
	ViewPort.Size = UDim2.new(0.3, 0, 0.4, 0)

	ViewPort.Parent = script.Parent

	ViewPort.BackgroundTransparency = 1

	ViewPort.Position = UDim2.new(0.35, 0,0.32, 0)
	local viewportCamera = Instance.new("Camera")

	viewportCamera.CameraType = Enum.CameraType.Scriptable
	ViewPort.CurrentCamera = viewportCamera

	fruitmodel:FindFirstChild("Part").CFrame = CFrame.new(viewpoint)
	fruitmodel.Parent = ViewPort
	local BLurEffect = Instance.new("BlurEffect")
	BLurEffect.Parent = game.Lighting
	BLurEffect.Size = 15

	local RSt = game:GetService("RunService").RenderStepped:Connect(function()

		local cframe,size = fruitmodel:GetBoundingBox()

		local max = math.max(size.X, size.Y, size.Z)

		local dis = (max/math.tan(math.rad(viewportCamera.FieldOfView))) * 1.5

		local currentdis = (max/2) + dis

		viewportCamera.CFrame = CFrame.new(viewpoint + Vector3.new(0,0,currentdis))

		local fullRotation = math.pi*2
		local currentRotation = tick()%fullRotation

		viewportCamera.CFrame = CFrame.Angles(0, currentRotation, 0) * CFrame.new(viewpoint + Vector3.new(0,0,currentdis),viewpoint)
	end)
	local TweenService = game:GetService("TweenService")
	--Adding in Rainbow text
	local Text = "Congratulations!"
	local RS = game:GetService("RunService")
	local rainbow = Instance.new("TextLabel")

	rainbow.Parent = script.Parent
	rainbow.BackgroundTransparency = 1
	rainbow.TextScaled = true
	rainbow.Position = UDim2.new(0.350, 0,0.189, 0)
	rainbow.Size = UDim2.new(0, 421,0, 50)
	rainbow.TextColor3 = Color3.fromRGB(255, 255, 255)	




	local Text2 = "You Found A Devil Fruit!!"
	local Bottemtext = Instance.new("TextLabel")
	Bottemtext.TextScaled = true
	Bottemtext.BackgroundTransparency = 1 
	Bottemtext.RichText = true
	Bottemtext.Parent = script.Parent
	Bottemtext.Position = UDim2.new(0.350, 0,0.607, 0)
	Bottemtext.Size = UDim2.new(0, 421,0, 50)
	Bottemtext.Text = Text2
	task.spawn(function()

		textanim.typeWrite(rainbow,Text, 0.01) 
	end)
	task.spawn(function()

		textanim.typeWrite(Bottemtext,Text2, 0.02)
	end)

	local grad = Instance.new("UIGradient")
	grad.Parent = rainbow
	grad.Rotation = 180
	local RS = game:GetService("RunService")
	local counter = 0       
	local w = math.pi / 12  
	local CS = {}          
	local num = 15 			
	local frames = 0		

	local hb = RS.Heartbeat:Connect(function()
		if math.fmod(frames, 2) == 0 then
			for i = 0, num do
				local c = Color3.fromRGB(127 * math.sin(w*i + counter) + 128, 127 * math.sin(w*i + 2 * math.pi/3 + counter) + 128, 127*math.sin(w*i + 4*math.pi/3 + counter) + 128)
				table.insert(CS, i+1, ColorSequenceKeypoint.new(i/num, c))
			end
			grad.Color = ColorSequence.new(CS)
			CS = {}
			counter = counter + math.pi/40
			if (counter >= math.pi * 2) then
				counter = 0
			end
		end
		if frames >= 1000 then
			frames = 0
		end
		frames += 1
	end)
	
	-- use task.delay to run a task.spawn() a desired amount of time later.
	task.delay(3, function()
		-- disconnect old connections
		hb:Disconnect()
		RSt:Disconnect()
		debounce = false
		fruitmodel.Parent = game.Players.LocalPlayer.Backpack
		remote:FireServer(playergui)

		Bottemtext:Destroy()
		rainbow:Destroy()
		ViewPort:Destroy()
		viewportCamera:Destroy()
		BLurEffect:Destroy()

		print("cheese")	
	end)
end


local connection 




for i,v in pairs(DevilFruits:GetChildren()) do
	v:FindFirstChild("Part").Touched:Connect(function(hit)
		if hit then
			print("cjeee")
			CreateDevilGui(v)
			print(debounce)
		end	
	end)
end

DevilFruits.ChildAdded:Connect(function(v)
	v:FindFirstChild("Part").Touched:Connect(function(hit)
		if hit then
			print("cjeee")
			CreateDevilGui(v)
			print(debounce)
		end	
	end)
end)
1 Like

i heard that task.spawn was trash and spawn would just be better guess they were wrong then