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

Is there a way to make a script that detects when its hit by a specific named block?

Asked by 3 years ago

Basically, I am trying to make a script that will activate a section of code whenever a specific part touches the part.

I am new to scripting so this is basically all I got.

if workspace.specificpart.Touched then
    workspace.doors:Destroy ()
end

Does anyone know how to help?

2 answers

Log in to vote
1
Answered by 3 years ago

Well, Touched isn't a boolean, it's a script signal. You'll get false because Touched exists as a valid signal but it doesn't return anything, so the statement reads it as nil.

If you want to know what Touched does, it's called as a script signal of the BasePart family (said family consists of Parts, NegativeParts, UnionOperations, etc.). When Touched fires, it passes the BasePart that made contact with the BasePart whose Touched event is listening. For example, if a player's character is R6 and their left leg hits the part listening for Touched, the passed BasePart will be Left Leg.

In order to associate any signal with any function (pre-written or post-written), Connect() is necessary. If no function was previously written for use with the signal, you can define this function within the signal:

BasePart.Touched:Connect(function(hit)
    -- some code
end)

Or, you can associate an existing function with it:

function foo()
    -- code
end

BasePart.Touched:Connect(foo) -- Using foo() with make Lua assume you're running the function. You do not want to use parenthesis if you're linking an existing function.

For the main issue at hand, every Instance has a property called Name, which is a string that represents the name given to the Instance. This property can be changed by the scripter at will; it's essentially a dynamic property. The rest is up to you.

0
Hmm, all it does is immediately executes the code. Am I making a mistake? Student20986 0 — 3y
0
Well, that means the signal is firing immediately. You should add if statements to check if the part is associated with anything relevant, like the part you want touching the listener. DeceptiveCaster 3761 — 3y
0
Ok I have it working, how do I get it to do a specific part, like spawnlocation Student20986 0 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

You can use the "hit" paramter of the .Touched event :

workspace.part.Touched:Connect(function(hit)
    if hit.Name = specifiPart.Name then 
        workspace.doors:Destroy()

    end
end)

Here if a part nammed lake the specificPartso if the specificPart tocuhes) the current part, we do actions

0
Hmm, i get a bunch of red lines under the = sign, and the word name and then Student20986 0 — 3y

Answer this question