You can write your topic however you want, but you need to answer these questions:
- What do you want to achieve? Keep it simple and clear!
I want to teleport my player and other players somewhere by pressing a button/key, and I want them to teleport only if they are near me within a certain distance.
-
What is the issue? Include screenshots / videos if possible!
Teleport - YouTube
They teleport to the destination no matter what the range.
Also I don’t really know how to script since I just started very recently so I can easily get confused in scripting.
- What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I tried searching it up in Devhub and devforums
Script:
local players = game:GetService(“Players”)
local replicated = game:GetService(“ReplicatedStorage”)
local remote = replicated.Teleport
local destination = game.Workspace.TeleportDestination
remote.OnServerEvent:Connect(function(player)
if not player then return end
local character = player.Character
if not character then return end
local root = character.PrimaryPart
if not root then return end
local humanoid = character:FindFirstChildOfClass(“Humanoid”)
if not humanoid then return end
if humanoid.Health <= 0 then return end
for _, otherPlayer in ipairs(players:GetPlayers()) do
if not otherPlayer then continue end
local otherCharacter = otherPlayer.Character
if not otherCharacter then continue end
local otherRoot = otherCharacter.PrimaryPart
if not otherRoot then continue end
local otherHumanoid = otherCharacter:FindFirstChildOfClass("Humanoid")
if not otherHumanoid then continue end
if otherHumanoid.Health <= 0 then continue end
local distance = player:DistanceFromCharacter(root.Position)
if distance <= 100 then
otherCharacter:SetPrimaryPartCFrame(destination.HumanoidRootPart.CFrame*CFrame.new(0,10,0))
end
end
end)
Local Script:
local userInput = game:GetService(“UserInputService”)
local replicated = game:GetService(“ReplicatedStorage”)
local remote = replicated:WaitForChild(“Teleport”)
userInput.InputBegan:Connect(function(key, processed)
if processed then return end
if key.KeyCode == Enum.KeyCode.Q then
remote:FireServer()
end
end)