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

is there any way for me to find out which part has activated the function?

Asked by
lytew 99
4 years ago

is there any way for me to find out which part was played? Whether it was Coin2 or Coin3? script:

local player = game.Players.LocalPlayer
function touch (hit)            
    print("touch")
        print("Part "..Coin3 or Coin2 "touched")
end
workspace.Coin3.Touched:Connect(touch)
workspace.Coin2.Touched:Connect(touch)

3 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago
local player = script.Parent
local humanoid = player:WaitForChild("Humanoid")

humanoid.Touched:Connect(function(hit)
    if hit.Name == "Coin2" then
        print("Coin2 was touched.")
    elseif hit.Name == "Coin3" then
        print("Coin3 was touched".)
    end
end)

This will print what coin was touched. Make sure you put this as a local script into StarterCharacterScripts!

0
the problem is that i needed it in one function lytew 99 — 4y
0
ok i will edit the answer with the new code in a second Dan_PanMan 227 — 4y
0
@lytew check the new code Dan_PanMan 227 — 4y
0
ok lytew 99 — 4y
View all comments (6 more)
0
what exactly does this script do? lytew 99 — 4y
0
oh man thanks! I'll try to edit it here, it will help a lot, really! lytew 99 — 4y
0
check if a coin was touched and prints out the coin that was touched Dan_PanMan 227 — 4y
0
be sure to accept the answer if it works Dan_PanMan 227 — 4y
0
There is no need to check the name of hit. You can concatenate the print() with the name of the hit part. LennyPlayzYT 269 — 4y
0
There is no need to check the name of hit. You can concatenate the print() with the name of the hit part. LennyPlayzYT 269 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I think it would be easier just to use tables to solve this problem.

local player = game.Players.LocalPlayer
local coins = {workspace:WaitForChild("Coin3"), workspace:WaitForChild("Coin2")}

for _,coin in pairs(coins) do
    coin.Touched:Connect(function(hit)
        print(coin.Name)
    end)
end

What's happening is I am taking the contents of the table and adding a touched event for each element of the table.

Log in to vote
0
Answered by 4 years ago

Hi Iytew! There is a much simpler way of doing this.

function onTouched(hit)
      print(hit.Name.. " was touched!")
end

script.Parent.Touched:Connect(onTouched)

We can get the name of the part that touched that specific part.

Hope I helped! LennyPlayzYT

0
I think he wants the actual part that was touched, not the part that touched it. My bad if I misunderstood though. Brandon1881 721 — 4y
0
Yeah, the actual part that touches is "hit" LennyPlayzYT 269 — 4y
0
We can do hit.Name.. " was touched!" meaning, it gets the name of the part. Let's say it was Coin2. It will say "Coin2 was touched!" LennyPlayzYT 269 — 4y

Answer this question