Simple fire spread script

How could i make a simple fire spread script like in Natural Disaster Survival?

3 Likes

Found this with a quick toolbox search, feel free to use it as a reference
(Original Model ID: 151654554)

	if hit == nil then
		return
	end
	if hit.Parent == nil then
		return
	end
	if hit.Anchored == true and hit.Locked == true then
		return end
	if hit.Name == script.Parent.Name then 
		return end

	if hit.Name == "Engine" then 
		return end	

	hit.Anchored = false
	hit.Name = script.Parent.Name
	hit.Color = Color3.new()
	
	local fire = Instance.new("Fire")
	fire.Parent = hit
	fire.Heat = 0
	fire.Size = 30

	wait(math.random(5,10))
	script:clone().Parent = hit

	wait(math.random(5,10))

	hit:BreakJoints()

	hit.Color = Color3.new("")

	wait(math.random(5,10))

	hit.Velocity = Vector3.new(0, 100, 0)

	hit.Size = hit.Size/Vector3.new(2,2,2)

	wait(math.random(5,10))
	
	hit:remove()
	
end

script.Parent.Touched:connect(OnTouched)
2 Likes

This works pretty well, except it only spreads when it touches a new part. How could i make it spread to surrounding parts that it’s already touching?

You can try using a distance instead of a .Touched function.

To get a distance, simply just subtract the two positions, then get the magnitude.

local part = workspace.FirePart

for _, otherPart in workspace:GetDescendants() do
    if otherPart:IsA('BasePart') and otherPart.Name ~= 'Terrain' then
        if (part.Position - otherPart.Position).Magnitude < 50 then -- Less than 50 means it is within 50 studs of the part.
            -- spread fire
        end
    end
end

That would be very bad on framerate if it gets called a lot.

I would recommend to use the :GetTouchingParts() function every 5 seconds to return a table of all the parts touching the fire part, and then check if the parts material is a burnable material (Grass, Wood, Planks etc) and then spread the fire to the part.

function SpreadFire(Part)
    for i, v in pairs(Part:GetTouchingParts()) do
        -- This script would only spread to Grass and Wood
        if v.Material == Enum.Material.Grass or v.Material == Enum.Material.Wood then
            local Cloned_Fire = Part.Fire:Clone()
            local Cloned_Fire_Code = script:Clone()
            Cloned_Fire.Parent = v
            Cloned_Fire_Code.Parent = v
        end
    end
end

while task.wait(5) do
    SpreadFire(script.Parent)
end
1 Like

I tried to implement this into the current script…

for _, v in pairs(script.Parent:GetTouchingParts()) do
	OnTouched(v)
end

script.Parent.Touched:Connect(function(obj)
	OnTouched(obj)
end)

It still only recognises moving parts, how could i fix this?

The :GetTouchingParts() returns a table of all parts touching a part at a given time, and if it is only called once, then it will only look for touching parts once, this is the reason i added the while wait() loop in my code, as it would constantly check for parts.