Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Magnitude or Radius, teleportation help?

I'm trying to make a script that allows the user to teleport with multiple people if they are within a certain magnitude or radius. However, i've tried both methods but can't seem to teleport both the user and the people around the user.

Any ideas?

Local Script


Debounce = false
local mouse = script.Parent.Parent:GetMouse()

game:GetService('UserInputService').InputBegan:connect(function(input, gameprossed)
if input.KeyCode == Enum.KeyCode.T and Debounce == false then
  Debounce = true

  local ReplicatedStorage = game:GetService("ReplicatedStorage")
  local Teleport = ReplicatedStorage:WaitForChild("Apparate")

  Teleport:FireServer(mouse.hit.p)

  wait(2)
  Debounce = false
end
end)

** Server Script 1 - Magnitude **

game.ReplicatedStorage.Apparate.OnServerEvent:Connect(function(player, MouseHit)
local radius = 5
local children = game.Players:GetChildren()
for i, child in pairs(children) do

    if (child.Character.Head.Position - player.Character.Head.Position).magnitude <= radius then
       child.Character.HumanoidRootPart.CFrame = CFrame.new(MouseHit)
       player.Character.HumanoidRootPart.CFrame = CFrame.new(MouseHit)
    end

end
end)

**Server script 2 - Radius **

game.ReplicatedStorage.Apparate.OnServerEvent:Connect(function(player, MouseHit)
local radius = 5
local children = game.Players:GetChildren()
for i, child in ipairs(children) do

if child:DistanceFromCharacter(Vector3.new(player.Character.HumanoidRootPart.CFrame) < radius then
    child.Character.HumanoidRootPart.CFrame = CFrame.new(MouseHit)
    player.Character.HumanoidRootPart.CFrame = CFrame.new(MouseHit)
  end

end
end)

I have tried both methods of Radius and Magnitude, (obviously at different times) and I can't seem to get them to work...

Any ideas?

Thank you in advance. + Merry Christmas!

3 comments

0

Have you tried my other method yet? I know it's not exactly what you wanted, but it should work.

0

Oh I know why this might not work, if a player is jumping, but also 5 studs away, then their head will be more than 5 studs away.

0

sorry, when I copied your script over, I forgot to remove the .magnitude off of the end of your lines

1 answer

✓ Accepted answer
game.ReplicatedStorage.Apparate.OnServerEvent:Connect(function(player, MouseHit)
local radius = 5
local children = game.Players:GetChildren()
for i, child in pairs(children) do

    if math.abs(child.Character.Head.Position.X - player.Character.Head.Position.X)<= radius or math.abs(child.Character.Head.Position.Z - player.Character.Head.Position.Z)<= radius then
       child.Character.HumanoidRootPart.CFrame = CFrame.new(MouseHit)
       player.Character.HumanoidRootPart.CFrame = CFrame.new(MouseHit)
    end

end
end)

So I changed it so it would only detect the position on the x and z axis, so that even if a player jumps, it should teleport them.

also pretty sure hit should be capitalized, so that one line in the local script should be Teleport:FireServer(mouse.Hit.p)

2 comments

0

Just tried this and i'm getting the following error: https://gyazo.com/47df28caac8142a8b6b98ff10e6399f6

0

Legend! It works, thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.