Script with highlights not working properly

So im making a game that when u touch parts they fall and u have to be the last one to survive and etc, but im running into a weird issue, when you touch any part it doesnt enable the highlight at all or sometimes it just randomly enables another highlight from another random part

for i,v in next, game.Workspace.FallBlocks:GetChildren() do
	coroutine.wrap(function()
		local debounce = false
		
		local fallBlock = v
		local hitbox = fallBlock.Hitbox
		local particles = fallBlock.Inner.ParticleEmitter
		
		local tweenService = game:GetService("TweenService")

		hitbox.Touched:Connect(function()
			local highlight = fallBlock.Highlight
			if debounce == false then
				debounce = true
				for i = 1, 3 do
					highlight.Enabled = true
					task.wait(0.35)
					highlight.Enabled = false
					task.wait(0.4)
				end
				
				task.wait(0.5)
				
				highlight.Enabled = true
				particles:Emit(35)
				
				local v1 = tweenService:Create(hitbox, TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut), {Position = Vector3.new(hitbox.Position.X, hitbox.Position.Y - 25, hitbox.Position.Z)})
				v1:Play()

				fallBlock.Outer.Transparency = 1
				fallBlock.Outer.CanCollide = false
				fallBlock.Inner.Transparency = 1
				fallBlock.Inner.CanCollide = false
				highlight.OutlineTransparency = 1

				local v2 = tweenService:Create(highlight, TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {FillTransparency = 1})
				v2:Play()
			end
		end)
	end)()
	
	task.wait(0.1)
end

You do not need a coroutine wrap around your function, it doesn’t yield only it’s connection does.

Your for loop is messed up, remove the next, part

for _, v in game.Workspace.FallBlocks:GetChildren() do
local debounce = false
local tweenService = game:GetService("TweenService")

for i,v in pairs(workspace.FallBlocks:GetChildren()) do
	if v.Hitbox then
		if v.Hitbox:IsA("Part") then
			
			local hitbox = v.Hitbox
			hitbox.Touched:Connect(function(hit)
				local particles = v.Inner.ParticleEmitter
				local highlight = v.Highlight
				local Player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
				if Player then
					if debounce == false then
						debounce = true
						for i = 1, 3 do
							highlight.Enabled = true
							task.wait(0.35)
							highlight.Enabled = false
							task.wait(0.4)
						end
						
						task.wait(0.5)

						highlight.Enabled = true
						particles:Emit(35)

						local v1 = tweenService:Create(hitbox, TweenInfo.new(1, Enum.EasingStyle.Quart, Enum.EasingDirection.InOut), {Position = Vector3.new(hitbox.Position.X, hitbox.Position.Y - 25, hitbox.Position.Z)})
						v1:Play()

						v.Outer.Transparency = 1
						v.Outer.CanCollide = false
						v.Inner.Transparency = 1
						v.Inner.CanCollide = false
						highlight.OutlineTransparency = 1
						
						local v2 = tweenService:Create(highlight, TweenInfo.new(0.25, Enum.EasingStyle.Linear, Enum.EasingDirection.In), {FillTransparency = 1})
						v2:Play()
					end
				end
			end)
		end
	end
end

Alr i did the changes to my code that u guys recommended me and its working pretty nice! but i still couldnt fix the highlights not working or the parts not falling at all

edited the code. Also you can only activate 1 part since you aren’t changing the debounce back to false

1 Like