Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Teleport players in radius of another player to location of mouse?

Asked by 5 years ago
Edited 5 years ago

I'm trying to create a script which allows the user to press "T" on their keyboard and teleport to wherever their mouse is. However, I've managed to cover that part but now i'm trying to use radius to check for any players within 5 studs of the user pressing "T" and if they are, teleport them along with the user to the location of the mouse position. It's my first time using radius detection and I can't seem to make it work... Anyone able to help?

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

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)

Thank you. :)

0
If you're wondering why I thought of such a weird solution, then let me tell you that my solution for getting user input to work with a plane was to put the entire plane model inside the character. SteamG00B 1633 — 5y
0
Well GopnikChan 0 — 5y

2 answers

Log in to vote
0
Answered by
SteamG00B 1633 Moderation Voter
5 years ago
Edited 5 years ago

This might be an out of the box solution, but you could put a cylinder with cancollide=false and transparency=1 with a radius of 5 in the player welded to their HumanoidRootPart when they load in, and then you would use an onTouch function to teleport everyone touching the player's cylinder to the mouse position when the player presses "T".

game:GetService('UserInputService').InputBegan:connect(function(input, gameprossed)
   if input.KeyCode == Enum.KeyCode.T and Debounce == false then
    Debounce = true
    script.Parent.cylinder.Touched:Connect(function(hit)
        hit.HumanoidRootPart.CFrame = CFrame.new(mouse.hit.p)
    end)
    wait(2)
    Debounce = false
   end
end)

It would be something along the lines of this, I know you don't have this set up this way, so you will have to do some minor tweaking to get it to work, but it should work.

I always think differently than most people, but if there is something actually wrong with doing it this way, then let me know.

0
I like the idea, but I would prefer it to be using radius rather than touch functions. - Just because i'm trying to learn how to use radius and I thought this would be a good idea to learn from. xXTouchOfFrostXx 125 — 5y
0
that is a good idea, but if you don't get a solution from someone here and you are still stuck, then I'd at least try this. I also actually don't know if this will work due to the touch function being inside a debounce. SteamG00B 1633 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

You should use magnitude. Look here for reference: https://developer.roblox.com/articles/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)

0
I tried to use the following code ^, yet it didn't teleport anyone around me to the location, except myself. xXTouchOfFrostXx 125 — 5y

Answer this question