Error using Instance.Touched

Hi, I am repliicated, and today I started working on an interactive showcase game, The Light. It has not been released to the public yet. Basically, you spawn in a bright white place, hence the name, and can choose to go back into the living world, or die. It is based off, “Don’t go into the light!” I’m using TeleportService to teleport the player into a game with a graveyard, if they choose death. If they choose life, they’re going to be waking up in a hospital bed. I’m getting this error when touching the death portal:


My code:

local life = workspace.Life
local death = workspace.Death
local tp = game:GetService("TeleportService")

death.Portal.Touched:Connect(function(player, touchPart)
	if touchPart.Name ~= "Baseplate" then
		tp:Teleport(6237172910, player)
	end
end)

life.Portal.Touched:Connect(function(player, touchPart) -- this part is not ready yet, I just copied the death part and yea
	if touchPart.Name ~= "Baseplate" then
		tp:Teleport(6237172910, player)
	end
end)

Help would (as always) be appreciated!

THE LIGHT IS RELEASED!!

.Touched only has 1 parameter which is the parameter marked as “player”

2 Likes

death.Portal.Touched:Connect(function(player, touchPart)

Are you sure you’re trying to index the Part that it touched? I think the first argument is supposed to be what it actually touched and the other arguments are just data to send in, so maybe you could do something like this?

death.Portal.Touched:Connect(function(touchPart)
    local Player = game.Players:GetPlayerFromCharacter(touchPart.Parent)
    if Player then
        tp:Teleport(6237172910, player)
    end
end

the touched event only has one parameter.

1 Like

The BasePart.Touched event takes only one parameter, which would be the part that touched the instance. Typically, you can find the parameter named hit or something like that. For example, if a character’s left leg touched the instance, then the first parameter would be the left leg.

Based on what you’re trying to do, perhaps this code will be more suiting:

death.Portal.Touched:Connect(function(hit)
    -- Check if it's an actual character touching the part
    if hit.Parent:FindFirstChild("Humanoid") then
        -- Now, get the actual player
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        -- Now, we'll just make sure the player exists. More than likely, it should, but it's safe to just check
        if player then
            tp:Teleport(6237172910, player)
        end
    end
end)

Hope this helps! Next time, I’d make sure you check the API Reference. For example, here’s the Touched Event.

1 Like

Made sense, when I deleted the touchpart it gave me an error.

Remember to mark the solution for others to easily view if anyone else shares this issue!

1 Like