Hi every time I teleport using this script I always fling around. It is supposed to teleport if you stay on it for 10 seconds. I added comments for you to understand
local part = script.Parent
local duration = 10 -- Total time in seconds to trigger effects
local fadeStartTime = 5 -- Time in seconds when the frame starts appearing
local targetPosition = CFrame.new(520.705, -1.292, -365.756)
local tweenService = game:GetService("TweenService")
local rs = game:GetService("ReplicatedStorage")
local players = game:GetService("Players")
-- Variables
local activePlayer = nil
local initialPosition = nil
local fadeStarted = false
local frame = nil
local progressTime = 0
local timerActive = false
local debounce = false
-- Function to create the white frame
local function createWhiteFrame(player)
local gui = Instance.new("ScreenGui", player.PlayerGui)
gui.IgnoreGuiInset = true
local frame = Instance.new("TextLabel")
frame.Size = UDim2.new(1, 0, 1, 0)
frame.BackgroundColor3 = Color3.new(1, 1, 1)
frame.BackgroundTransparency = 1
frame.Text = ""
frame.Parent = gui
return frame
end
-- Function to safely teleport a character
local function safeTeleport(character, targetCFrame)
local rootPart = character:FindFirstChild("HumanoidRootPart")
if not rootPart then return end
-- Temporarily anchor the root part
rootPart.Anchored = true
-- Reset velocity to prevent flinging
rootPart.Velocity = Vector3.zero
rootPart.RotVelocity = Vector3.zero
-- Teleport the character
rootPart.CFrame = targetCFrame
-- Wait a moment to ensure stability, then unanchor
task.wait(0.1)
rootPart.Anchored = false
end
-- Function to handle effects and teleportation
local function handleEffects(player)
local character = player.Character
if not character then
warn("Character not found for player:", player.Name)
return
end
-- Change atmosphere and sky
game.ReplicatedStorage.ChangeSkyToCave:FireClient(player)
-- Play music
game.Workspace.Charm.CaveMusic:Play()
-- Add hero effect
local clone = rs.BallParticles.Hero:Clone()
local torso = character:FindFirstChild("Torso")
if torso then
clone.Parent = torso
local weld = Instance.new("WeldConstraint", torso)
weld.Part0 = torso
weld.Part1 = clone
clone.Name = "enchantEffect"
-- Emit particles
clone.Attachment.Flare:Emit(1)
wait(0.2)
clone.Attachment.Circles:Emit(9)
clone.Attachment.Rays:Emit(1)
end
-- Teleport the character
safeTeleport(character, targetPosition)
end
-- Function to reset the process
local function reset()
if frame then
frame.Parent:Destroy()
frame = nil
end
activePlayer = nil
initialPosition = nil
fadeStarted = false
progressTime = 0
timerActive = false
print("Reset complete.")
end
-- Heartbeat connection for tracking
game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
if not activePlayer or not initialPosition or not timerActive then return end
-- Check player position
local character = activePlayer.Character
if character then
local rootPart = character:FindFirstChild("HumanoidRootPart")
if rootPart then
-- Check if the player has moved
if (rootPart.Position - initialPosition).Magnitude > 1 then
print("Player moved, resetting...")
reset()
return
end
end
end
-- Update progress time
progressTime += deltaTime
-- Start fading the frame after 5 seconds
if progressTime >= fadeStartTime and not fadeStarted then
fadeStarted = true
local tweenInfo = TweenInfo.new(duration - fadeStartTime, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
local tween = tweenService:Create(frame, tweenInfo, { BackgroundTransparency = 0 })
tween:Play()
print("Fade started.")
end
-- Trigger effects after 10 seconds
if progressTime >= duration then
print("Duration complete, triggering effects...")
handleEffects(activePlayer)
reset()
end
end)
-- Detect when a player touches the part
part.Touched:Connect(function(hit)
if debounce then return end -- Prevent duplicate triggers
debounce = true
local character = hit.Parent
local humanoid = character:FindFirstChild("Humanoid")
if humanoid then
local player = players:GetPlayerFromCharacter(character)
if player and player ~= activePlayer then
print("Player started standing on part.")
activePlayer = player
-- Start grace period
task.delay(1, function()
if activePlayer == player then
local rootPart = character:FindFirstChild("HumanoidRootPart")
if rootPart then
initialPosition = rootPart.Position -- Set initial position after grace period
print("Grace period ended, starting timer.")
timerActive = true
frame = createWhiteFrame(player)
end
end
end)
end
end
-- Reset debounce after cooldown
task.delay(15, function() debounce = false end)
end)
kinda weird bug lol