Why do I freeze?

I am trying to make a teleport service inside my game and everytime i step on a part to teleport I crash. Whats the problem?

Here is the code.

local teleport = game:GetService("TeleportService")
local part = script.Parent


local isData = {
	crouch = false
}

part.Touched:Connect(function(hit)
	local human = hit.Parent:FindFirstChild("Humanoid")
	if human then
		local char = hit.Parent
		local player = game.Players:GetPlayerFromCharacter(char)
		teleport:Teleport(5660471859, player, isData)
	end
end)

Any help?

It’s probably because you don’t have a debounce which is resulting in the script firing more than once.

How could I create a debounce for this? I am just trying to keep it as simplistic as possible.

just some delay and set it back to false or true

Generally, for Touched events, you always want to use a debounce to prevent a code from running more than once. A way you can do it is like this:

local debounce = true

part.Touched:Connect(function(hit)
	if not debounce then
        return
    end

    debounce = false
    
    --code here

    wait(2) --Adding a delay to so it doesn't fire more than once.

    debounce = true
end)

If you would like to view the documentation, it can be found here at the DevHub, where they provide more examples.

1 Like

Worked like a charm. Thank you good sir :slight_smile:

1 Like