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. :)
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.
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)