Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local player = Players.LocalPlayer
- local character = player.Character or player.CharacterAdded:Wait()
- local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
- local humanoid = character:WaitForChild("Humanoid")
- local debounce = false
- local teleportRange = 15 -- Distance to teleport away from dangerous objects
- local debounceTime = 0.1 -- Time delay to prevent rapid teleporting
- -- Function to calculate a safe position to teleport to
- local function getSafeTeleportPosition(dangerousObject)
- -- Get the direction from the dangerous object to the player
- local direction = (humanoidRootPart.Position - dangerousObject.Position).Unit
- -- Calculate a new position that's teleportRange studs away from the dangerous object
- local safePosition = humanoidRootPart.Position + direction * teleportRange
- -- Return the safe position
- return safePosition
- end
- -- Function to teleport the player to a safe position
- local function teleportAway(dangerousObject)
- -- Get the safe teleport position
- local safePosition = getSafeTeleportPosition(dangerousObject)
- -- Teleport the player's HumanoidRootPart to the safe position
- humanoidRootPart.CFrame = CFrame.new(safePosition)
- end
- -- Function to determine if a part is dangerous based on certain criteria
- local function isDangerousPart(part)
- local partName = part.Name:lower()
- -- Check if the part's name contains 'precast', 'hitbox', or other known danger indicators
- return part:IsA("BasePart") and (partName:find("precast") or partName:find("hitbox") or partName:find("danger"))
- end
- -- Function to continuously detect dangerous objects and teleport the player
- local function detectAndTeleport()
- if debounce then return end
- debounce = true
- -- Look for dangerous parts in the workspace
- for _, obj in pairs(workspace:GetDescendants()) do
- -- Check if it's a dangerous part
- if isDangerousPart(obj) then
- local distance = (humanoidRootPart.Position - obj.Position).Magnitude
- -- If within range, teleport away from the dangerous object
- if distance <= teleportRange then
- teleportAway(obj)
- break -- Exit after teleporting away from one object
- end
- end
- end
- debounce = false
- task.wait(debounceTime)
- end
- -- Function to set up the teleport detection for the current character
- local function setupTeleportDetection()
- while true do
- detectAndTeleport()
- task.wait(0.05) -- Frequent checks for dangerous objects
- end
- end
- -- Function to reconnect the dodge mechanism when the player respawns
- local function onCharacterAdded(newCharacter)
- character = newCharacter
- humanoidRootPart = character:WaitForChild("HumanoidRootPart")
- humanoid = character:WaitForChild("Humanoid")
- task.spawn(setupTeleportDetection) -- Start detecting and teleporting again after respawn
- end
- -- Initial setup for the current character
- task.spawn(setupTeleportDetection)
- -- Reapply the dodge logic when the player respawns
- player.CharacterAdded:Connect(onCharacterAdded)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement