Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

How can a script know if two parts are touching?

Asked by 8 years ago

This is the current script. It works in a sense but doesn't do what I want it to do.

local Target = script.Parent.Spreadable
local touching = Target:GetTouchingParts()

function makefire(parent)
    local fire = Instance.new("Fire")
    fire.Parent = parent
end

for _, part in next, touching do
    wait(1)
    makefire(part)
end

I'm trying to make a series of parts and I want the fire to spread to the part that it's touching if it has the same name, "spreadable". They are all inside a model but this only spreads the fire if the parts are inside of each other. I'm trying to make this work if the parts are touching. I tried the touched() function but that doesn't work either unless there is a collision. How can I fix this? Thanks.

0
What do you mean touching but the touch event doesn't work? GoldenPhysics 474 — 8y
0
Since fire only has to be near something to make it light up, you could try using a for loop and Magnitude. TheDeadlyPanther 2460 — 8y

1 answer

Log in to vote
0
Answered by
IcyEvil 260 Moderation Voter
8 years ago

I would use an Anonymous Function to do this.

Alright so we start by using an anonymous function and declaring what fire is.

script.Parent.Touched:connect(function(hit)
local f = script.Parent:findFirstChild("Fire")

end)

Now, we use the .Touched event in order to signal that IF the object the script is inside is either touched or is touching another object it will do something. we also declare that f is fire because, we do not need to create the fire within the script it could later on cause complications.

Now lets move on to what happens WHEN this object is touched or touches something.

script.Parent.Touched:connect(function(hit)
local f = script.Parent:findFirstChild("Fire")

if hit.Parent:IsA("Part") then
x = f:Clone()
if hit.Parent.Name == string.lower("spreadable") then

end
end
end
end)

Now you might ask Why are we using an :IsA in the script, well this just checks if the object it touched is a Part, we also made a new variable called x that is now a Clone of Fire, we also checked to see if the thing that hit or was hit by the objects name was "Spreadable" string.lower is just fancy terms so that the name could be like this "SPreADabLE" now lets make something happen AFTER it checks if it is a part and if the name is spreadable

script.Parent.Touched:connect(function(hit)
local f = script.Parent:findFirstChild("Fire")

if hit.Parent:IsA("Part") then
x = f:Clone()
if hit.Parent.Name == string.lower("spreadable") then
x.Parent = hit.Parent
end
end
end
end)

now we made the variable x's parent hit.Parent which means the new parent of the cloned fire is what touched the brick

That is a simple way of making it spread from one brick to another.

Ad

Answer this question