Whats wrong with my FireSpread Script?

This is a small project I’m working on making a fire system similar to the one in Natural Disaster Survival. Everything works great but the fire won’t spread. I’ve tried to use ChatGPT for hours trying to fix it and it doesn’t work. I’ve tried different ways to make the fire spread:

  1. When a part touches a fire part
  2. When a part is 3 studs or closer to a fire part (using rn)

It doesn’t give me any warnings or errors. Please help.

This is my script:

local function deleteWelds(part, model)
	for _, modelPart in pairs(model:GetDescendants()) do
		for _, weld in pairs(modelPart:GetChildren()) do
			if weld:IsA("WeldConstraint") then
				if weld.Part1 == part or weld.Part0 == part then
					weld:Destroy()
				end
			end
		end
	end
end

local function setPartOnFire(part, model)
	if part:FindFirstChild("Burning") then return end
	
	part.Name = "FirePart"

	local burning = Instance.new("BoolValue")
	burning.Name = "Burning"
	burning.Parent = part

	local fire = Instance.new("Fire")
	fire.Size = 25
	fire.Parent = part

	part.Material = Enum.Material.Neon
	part.Color = Color3.fromRGB(255, 200, 25)

	local colors = {
		Color3.fromRGB(255, 200, 25),
		Color3.fromRGB(240, 121, 1),
		Color3.fromRGB(240, 69, 21),
		Color3.fromRGB(41, 42, 42)
	}

	local burnTime = 20
	local stageTime = burnTime / 4

	for i = 2, 4 do
		wait(stageTime)
		part.Color = colors[i]

		if i == 3 then
			part.Anchored = false
			deleteWelds(part, model)
		end

		if i == 4 then
			fire:Destroy()
		end
	end

	part.Color = colors[4]
	burning:Destroy()
end

local function spreadFire(part, model)
	for _, otherPart in pairs(model:GetChildren()) do
		if not otherPart:FindFirstChild("Burning") and (otherPart.Position - part.Position).Magnitude <= 3 then
			wait(1)
			setPartOnFire(otherPart, model)
		end
	end
end

local tower = game.Workspace:WaitForChild("Tower")

local function startFire(model)
	local partsInModel = model:GetChildren()
	local randomPart = partsInModel[math.random(1, #partsInModel)]
	setPartOnFire(randomPart, model)
end

startFire(tower)

while true do
	wait(1)
	for _, part in pairs(tower:GetChildren()) do
		if part:FindFirstChild("Burning") then
			spreadFire(part, tower)
		end
	end
end

Edit: It seems like theres a problem with the StartFire() function, I tried running it multiple times and it kept picking the exact same part in the model of hundreds of other parts.

I also tried doing it multiples times like this:

local repeatTimes = 50
for i = 1, repeatTimes do
	startFire(tower)
end

It only did it once.

after testing it a bunch startFire() function is stopping the code before it gets to the while loop, i just added a task.spawn()

task.spawn(function()
	startFire(tower)
end)

also im not trying to bother but i wouldnt use wait() because its deprecated use task.wait()

If I remember correctly, Part.Touched itself only activates when the parts collide (ie. if they didn’t move and were already overlapping the event won’t fire)

As for the Magnitude/distance solution, the position refers to the center of the part, not the part iself. You might be able to use :GetPartBoundsinBox() to solve this by creating a box that is 3 studs wider in all directions.

local function spreadFire(part, model)
	-- // :GetPartBoundsInBox returns an array of all parts that are within a certain size/position.
	local boundingBox = workspace:GetPartBoundsInBox(part.CFrame, part.Size + Vector3.New(3,3,3)

	for _,otherPart: Part in pairs(boundingBox)) do
		if not otherPart:FindFirstChild("Burning") then
			task.wait(1)
			setPartOnFire(otherPart, model)
		end
	end
end

Please tell me if something goes wrong with this.

yea, I think i’ve been having more issues using wait(), thanks

I got this error:
ServerScriptService.FireScript:58: attempt to call a nil value

on this line:

local boundingBox = workspace:GetPartBoundsInBox(part.CFrame, part.Size + Vector3.New(3,3,3))

Also i’ve never used GetPartBoundsInBox() so in not sure how to use it.

My bad, I believe it’s because I accidentally used a capital N rather than lowercase in Vector3.new()
Try

local boundingBox = workspace:GetPartBoundsInBox(part.CFrame, part.Size + Vector3.new(3,3,3))

Yes it was lol, I don’t know how i missed that. Thanks a lot, really appreciate it!