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

Retrieving Teleport that has been touched?

Sorry I haven't long woken up and I know it's probably an easy fix but I can't for the life of me think straight...

local TpLocations = game:GetService("Workspace"):WaitForChild("TeleportFolder")

--DoesTheMagic
local function TeleportPlayer(HitPart)
	if HitPart.Parent:FindFirstChild("Humanoid") then
		
		print("A user has touched a TP: ".. HitPart.Parent.Name)
	end 
end


--FireFunctionOnTPTouch
for _,v in pairs(TpLocations:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(TeleportPlayer)
	end
end

I'm creating a folder which stores all Teleport Pads in the same location. On touch of any Teleport Pad, I would like it to print which Player has touched the Teleport Pad but also display which Teleport Pad has been touched.

Eg.

"A user has touched a TP: xXTouchOfFrostXx" "Teleport Location: _______ "

Any ideas?

Thank you :)

1 answer

✓ Accepted answer

You can create local function that will fire on [.Touched] and then will call [TeleportPlayer] with all arguments you need it to:

-- use ipairs for arrays
for _,v in ipairs(TpLocations:GetChildren()) do
    if v:IsA("BasePart") then
	local function _onTouched(HitPart)
		TeleportPlayer(HitPart, v) -- or v.Name
	end

        v.Touched:Connect(_onTouched)
    end
end

I named those local functions like private variables, but that's just my own style you don't have to do that.

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