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

How to make touch event on parts? [closed]

Asked by 4 years ago

I cant find anything about part touching part. Im talking about that if part touched another part then something happens . Plz write in answers. :-

Closed as Not Constructive by itz_rennox, Torren_Mr, User#29913, zblox164, and JesseSong

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
2
Answered by 4 years ago

To do a touched event, you could write it like this (as an anonymous function).

--// This script must be inside of your part

script.Parent.Touched:Connect(function(hit) --// If the script's parent (the part) is touched then it will connect this function and log what hit it (logged through the "hit" parameter).

-- Put what happens here (i.e. transparency changes, brickcolor changes, etc.

end)
0
THX! i dont know why i dont knowed about that despite that i knowed about the touched event but THX for realising about that! JasiuJasiuB 13 — 4y
0
Please accept my response if it helped you/solved your problem. :) ParticularlyPenguin 71 — 4y
Ad
Log in to vote
0
Answered by
Axenori 124
4 years ago

Code you need:

function OnTouch(hit)
--your code here
script.Parent.Touched:Connect(OnTouch)

Example of use (my fire script):

local val = 0
function OnTouch(hit)
    local head = hit.Parent:FindFirstChild("Head")
    local human = hit.Parent:FindFirstChild("Humanoid")
    if human ~= nil then
        local GC = hit.Parent:GetChildren()
        for i, v in pairs(GC) do
            if v:IsA("BasePart") then
                if v:FindFirstChild("FireEmmiter") == nil then
                local Particle = script.Parent:FindFirstChild("FireEmmiter")
                local ParticleClone = Particle:Clone()
                ParticleClone.Parent = v
                val = val + 1
                if val == 16 then
                val = 0
                    while true do
                        wait(0.025)
                        human.Health = human.Health - 1
                        if human.Health == 0 then
                            local s = hit.Parent:FindFirstChild("Shirt")
                            local p = hit.Parent:FindFirstChild("Pants")
                            for i, v in pairs(GC) do
                                if v:IsA("Accessory") then
                                    v:ClearAllChildren()
                                else if v:IsA("BasePart") then
                                v.Material = Enum.Material.Slate
                                v.Color = Color3.fromRGB(27, 42, 53)
                                if s ~= nil then
                                    s:Destroy()
                                if p ~= nil then
                                    p:Destroy()
                                    end
                                end
                                end
                            end
                        end
                        end
                    end
                    end
                end 
                end
                end
            end
            end
script.Parent.Touched:Connect(OnTouch)
0
i dont understand the code but the answer is helpful cause i can learn about that and then i will understand the code :D JasiuJasiuB 13 — 4y