Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer
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!

10 comments

0

the problem is that i needed it in one function

0

ok i will edit the answer with the new code in a second

0

@lytew check the new code

0

ok

0

what exactly does this script do?

0

check if a coin was touched and prints out the coin that was touched

0

oh man thanks! I'll try to edit it here, it will help a lot, really!

0

be sure to accept the answer if it works

0

There is no need to check the name of hit. You can concatenate the print() with the name of the hit part.

0

There is no need to check the name of hit. You can concatenate the print() with the name of the hit part.

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.

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

3 comments

0

I think he wants the actual part that was touched, not the part that touched it. My bad if I misunderstood though.

0

Yeah, the actual part that touches is "hit"

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!"

This discussion is preserved as it was. New answers, comments, and votes are closed.