Trouble with Building Collapse Trigger When Flying

So I want the building to collapse when it touches a player thats flying at it.But it doesnt work.I asked multiple AI’s for debugging but it still doesnt work.As you can see in the video below my characters FlyMoving value is true but the script doesnt think it is.

script.Parent.Touched:Connect(function(hit)
	print("Touched event triggered") -- Debugging

	local character = hit.Parent
	if character and character:FindFirstChild("Humanoid") then
		print("Humanoid detected in:", character.Name)

		local flyScript = character:FindFirstChild("FlyScript")
		if flyScript then
			print("FlyScript found in character")

			local flyMoving = flyScript:FindFirstChild("FlyMoving")
			if flyMoving and flyMoving:IsA("BoolValue") then
				print("Flymoving Value:", flyMoving.Value)

				local collapsed = script.Parent.Parent:FindFirstChild("collapsed")
				if collapsed and collapsed:IsA("BoolValue") then
					print("Collapsed Value:", collapsed.Value)

					if flyMoving.Value == true and collapsed.Value == false then
						print("Triggering collapse sequence...")

						-- Play collapse sound
						local collapseSound = script.Parent.Parent:FindFirstChild("collapse")
						if collapseSound and collapseSound:IsA("Sound") then
							collapseSound:Play()
							print("Collapse sound played")
						end

						-- Unanchor parts
						for _, obj in ipairs(script.Parent.Parent.top:GetChildren()) do
							if obj:IsA("Part") then
								obj.Anchored = false
							end
						end
						for _, obj in ipairs(script.Parent.Parent.bottom:GetChildren()) do
							if obj:IsA("Part") then
								obj.Anchored = false
							end
						end

						-- Set collapsed state
						collapsed.Value = true
						print("collapsed")
					end
				else
					print("Collapsed missing or not a BoolValue")
				end
			else
				print("Flymoving missing or not a BoolValue")
			end
		else
			print("FlyScript NOT found in character")
		end
	end
end)

and this is the output

Touched event triggered - Server - Script:2
Humanoid detected in: sozenss - Server - Script:6
FlyScript found in character - Server - Script:10
Flymoving Value: false - Server - Script:14
Collapsed Value: false

Is your fly script local? You may be changing flyMoving’s value on the client, which the server cannot see.

It is a local script.In that case i need to use a remote event right?

2 Likes