✓ Accepted answer
You need to have a variable with the closest object's position in it and if you find a closer object putting it into that variable. Then, when the loop is done, teleport the player to the object in the variable. You can use Magnitude to find the distance. Here is an example:
local players = game:GetService("Players")
local player = players.LocalPlayer
local closestDistanceFound
local teleportPosition
local character = player.CharacterAdded:Wait()
local humanoidRootPart = character.HumanoidRootPart
wait(5)
for i,v in pairs(game.Workspace:GetDescendants()) do
if v.Name == "Object" then
local distanceFromPlayer = (v.Position - humanoidRootPart.Position).Magnitude
if not closestDistanceFound then
if closestDistanceFound > distanceFromPlayer then
closestDistanceFound = distanceFromPlayer
teleportPosition = v.Position
end
else
closestDistancFound = distanceFromPlayer
teleportPosition = v.Position
end
end
end
humanoidRootPart.CFrame = CFrame.new(teleportPosition)
I hope this helps and have a great day scripting!
5 comments
When they say "include a script to show you've attempted to solve your problem", they don't actually mean to add a random script that has nothing to do with it, also, it's not that hard to find
^ lol
I Need Help.
Dude, I answered your question. Just scroll down this page.
If I helped you out please accept my answer. It is considered a courtesy if someone helps you out. If you don't end up accepting my answer, at least accept the answers that other people give you in the future.