Hi DevForum, I came across a question that I would like to address. How would I be able to tp the player to their mouse position, but only within a certain range, that’s obviously not too far?
I know I can do something like this:
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
-- Some code to trigger an event
game.ReplicatedStorage.RemoteEvent:FireServer(mouse.Hit.Position)
And then tp them from there.
So how would I, lets say under a radius of 10, be able to tp a player to their mouse?
local remoteEvent = script.Parent:WaitForChild("OnTeleportClick")
local Radius = 10
remoteEvent.OnServerEvent:Connect(function(plr, pos)
local char = plr.Character
local hrp = char:FindFirstChild("HumanoidRootPart")
if not hrp then return end
hrp.Position += (pos - hrp.Position).Unit * Radius
end)
–Local script
local remoteEvent = script.Parent:WaitForChild("OnTeleportClick")
local tool = script.Parent
local mouse = game.Players.LocalPlayer:GetMouse()
tool.Activated:Connect(function()
local pos = mouse.Hit.Position
remoteEvent:FireServer(pos)
end)
Sorry to bother you, how would I make it so they can teleport anywhere, but the maximum radius is 10? Like they can teleport anywhere under the maximum radius?
Basically, the code I gave you would tp the player to the direction he aimed, but only within the radius.
Meaning, anywhere they’d point at, they’d only tp within the radius 10.
If you want 10 to be the maximum radius, you could do something like this :
local remoteEvent = script.Parent:WaitForChild("OnTeleportClick")
local Radius = 10
remoteEvent.OnServerEvent:Connect(function(plr, pos)
local char = plr.Character
local hrp = char:FindFirstChild("HumanoidRootPart")
if not hrp then return end
hrp.CFrame = CFrame.lookAt(hrp.Position,Vector3.new(pos.X,hrp.Position.Y,pos.Z))
if (pos - hrp.Position).Magnitude <= Radius then
hrp.Position = pos -- will tp them to where he pointed at
else
hrp.Position += ((pos - hrp.Position).unit) * Radius -- will move them 10 studs towards the point they aimed
end
end)
Test it out and tell me if it works, for me it did.
Hello, I have an issue with this code, the player teleports anywhere the mouse points to which includes the sky and id rather not have this, it also teleports you through the floor if you were to click on a spot close to you. Is there a fix?