Random teleport after reset

i use this script for normal teleport

local CurrentLocation = script.Parent
local Destination = game.Workspace.Teleport2
-- (Optional), You can create local variable for each part.
CurrentLocation.Touched:Connect(function(Touch)
	-- Touched function.

	if Touch.Parent:FindFirstChild("HumanoidRootPart") then
		-- Checks if the touched part parent contains 'HumanoidRootPart'
		-- and if it does then this line of code will run.
		local startPosition = Destination.Position - Vector3.new(Destination.Size.X / 2, Destination.Size.Y / 2, Destination.Size.Z / 2)
		local endPosition = Destination.Position + Vector3.new(Destination.Size.X / 2, Destination.Size.Y / 2, Destination.Size.Z / 2)
		local position = Vector3.new(math.random(startPosition.X, endPosition.X), math.random(startPosition.Y, endPosition.Y), math.random(startPosition.Z, endPosition.Z))
		Touch.Parent:FindFirstChild("HumanoidRootPart").Position = position 
		-- This teleports the player to the destination.

	end

end)

it’s not my code, but it’s good for my idea…but, this script have a big problem, when I reset i die, I’m teleported extremely randomly, how do i solve?

2 Likes

Do you either spawn on this part or do your body parts touch it when you die?

3 Likes

I don’t know what the problem is yet :frowning:

1 Like

Can you show a video of what happens?

I mean you could just add a check to see if the player is alive before teleporting:

local CurrentLocation = script.Parent
local Destination = game.Workspace.Teleport2

CurrentLocation.Touched:Connect(function(Touch)
    local humanoid = Touch.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid and humanoid.Health > 0 then
        if Touch.Parent:FindFirstChild("HumanoidRootPart") then
            local startPosition = Destination.Position - Vector3.new(Destination.Size.X / 2, Destination.Size.Y / 2, Destination.Size.Z / 2)
            local endPosition = Destination.Position + Vector3.new(Destination.Size.X / 2, Destination.Size.Y / 2, Destination.Size.Z / 2)
            local position = Vector3.new(math.random(startPosition.X, endPosition.X), math.random(startPosition.Y, endPosition.Y), math.random(startPosition.Z, endPosition.Z))
            Touch.Parent:FindFirstChild("HumanoidRootPart").Position = position 
        end
    end
end)

You are randomizing a position to teleport to, why wouldn’t you be teleported randomly?

I can’t video because it’s too long, but I’ll try with words… I teleport as I should, but when I have to reset or when I die, I teleport again randomly

just show a video of when you reset, I cant understand what you are saying.

I hope it is understood, but the idea is that this happens after passing through the portal

it doesn’t work the way I want, it’s a video that shows the problem, I hope you understand

please just show the full video and not you character being resetted, try to share it with other websites or compress this cutting the parts of the video that isnt essential

I tried with a link on youtube

try with a debounce:

local TeleportDebounce = true --this adds a debounce
local CurrentLocation = script.Parent
local Destination = game.Workspace.Teleport2

CurrentLocation.Touched:Connect(function(Touch)
    local humanoid = Touch.Parent:FindFirstChildOfClass("Humanoid")
    if humanoid and TeleportDebounce  then -- this checks if the debounce is true, if not, it wont run
        if Touch.Parent:FindFirstChild("HumanoidRootPart") then
            TeleportDebounce = false -- set the variable to false, so that the code wont run until this become true
            local startPosition = Destination.Position - Vector3.new(Destination.Size.X / 2, Destination.Size.Y / 2, Destination.Size.Z / 2)
            local endPosition = Destination.Position + Vector3.new(Destination.Size.X / 2, Destination.Size.Y / 2, Destination.Size.Z / 2)
            local position = Vector3.new(math.random(startPosition.X, endPosition.X), math.random(startPosition.Y, endPosition.Y), math.random(startPosition.Z, endPosition.Z))
            Touch.Parent:FindFirstChild("HumanoidRootPart").Position = position 
        end
    end
     task.wait(5) --change this to whatever you like
     TeleportDebounce  = true -- this will return the debounce to true, so the code will run after x seconds
end)

the debounce is basically a boolean variable ( in this case ) to make sure the code wont run until the variable is set to true ( or false ), this is very used and maybe you heard of this.

I tried and it doesn’t work, I keep teleporting randomly, I want the teleportation to work only when I enter the portal

are you sure is the only script the part has for the teleport?

try to disable this script and check if it stills teleport

it is only one script that uses teleportation, so it has no way to collide with other teleports

try to disable the script after being teleported

I tried with enabled false and the problem persists, I don’t know how

enabled false for script, not for parts

search all scripts you have, and delete everything if necessary