Place teleportation hit.parent doesnt work?

Hello all,

I’m trying to teleport the player who touched a part in workspace to another place, it gives me an error though:
image

My code:

local TeleportService = game:GetService("TeleportService")
local PlaceID = 5626946530

script.Parent.Touched:Connect(function()
	wait(0.1)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		TeleportService:Teleport(PlaceID, player)
	end
end)

What did I do wrong?

1 Like
local TeleportService = game:GetService("TeleportService")
local PlaceID = 5626946530

script.Parent.Touched:Connect(function(hit)
	wait(0.1)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if player then
		TeleportService:Teleport(PlaceID, player)
	end
end)

Forgot the “hit” parameter, simple mistake.

1 Like

Add hit after function()

As said Phoenix, you simply forgot to index hit from the function.

Just a reminder, if you are teleporting places to a third party game, you need to enable “Third Party Teleports” feature in the project settings.

One more note: TeleportService does not work in studio.

Alright I’ll try it with the “hit” :slight_smile: thanks

1 Like

Thanks for reminding, :slight_smile: I know it and I’ll keep it in mind

1 Like

Thanks for the answer before, but, would you also know how I could make a debounce for this?
I tried inserting a wait(1) like this to make it unlag(but it still crashed roblox):

local TeleportService = game:GetService("TeleportService")
local PlaceID = 5626946530

script.Parent.Touched:Connect(function(hit)
    wait(0.1)
    local player = game.Players.GetPlayerFromCharacter(hit.Parent)
    if player then
       TeleportService:Teleport(PlaceID, player)
    end
end)

How should  add a debounce?
1 Like

Well I assume you want to debounce it like so, use a table so multiple players can use it.

local TeleportService = game:GetService("TeleportService")
local PlaceID = 5626946530
local DBData = {}

script.Parent.Touched:Connect(function(hit)
    local player = game.Players.GetPlayerFromCharacter(hit.Parent)
    if player and DBData[player] == nil then
       DBData[player] = player
       TeleportService:Teleport(PlaceID, player)
    end
end)

1 Like

I haven’t yet learned about tables, would this automatically make a debounce sort of thing?

1 Like

Well it doesn’t automatically make it debounce

At
DBData[player] == nil
It checks if the player is in the table, if it’s not, then it continues

At
DBData[player] = player
It adds the player to the table so they don’t get teleported again

1 Like

So if I get it right it does make sure one player doesnt ‘hit’ twice?

1 Like

Yes that is what a debounce does and that is what that code posted above does.

1 Like

Alright, a big thank you for this :slight_smile:

1 Like