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

How to teleport player to a point they clicked on a mini map?

I have rendered the entire map into a bird's-eye view image, and have it as an ImageButton. Here's my weak attempt at the question:

local Players = game:GetService('Players')
local localPlayer = Players.LocalPlayer
local UserInputService = game:GetService('UserInputService')
local button = localPlayer.PlayerGui:WaitForChild('Gui').Map

button .MouseButton1Click:Connect(function()
	local relativeMousePos = UserInputService:GetMouseLocation() - button.AbsolutePosition
	local pos = Vector3.new(relativeMousePos.X, 200, relativeMousePos.Y)
	print(pos)
	localPlayer.Character.HumanoidRootPart.CFrame = CFrame.new(pos)
end)

While it certainly does teleport, but neither does the scale or the position aligns with the actual map.

I have seen these posts:

DevForum: 2D positioning to 3D positioning

DevForum: How would I get the world position of a GUI properly?

DevForum: How to find the cursor position inside a gui object

However I cannot understand them well enough to combine and apply them to my situation.

1 answer

Try using Mouse.Hit.Position or UserInputService and rays.

1. Mouse

--LocalScript in StarterPlayerScripts

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()

local RemoteEvent = game:GetService(“ReplicatedStorage”):WaitForChild(“TeleportClick”) --if you don’t have this RemoteEvent in the ReplicatedStorage, create one because it will cause an error

Mouse.Button1Down:Connect(function()
   local position = Mouse.Hit.Position
   RemoteEvent:FireServer(position)
end)

--Script in ServerScriptService

local RemoteEvent = game:GetService(“ReplicatedStorage”):WaitForChild(“TeleportClick”) --if you don’t have this RemoteEvent in the ReplicatedStorage, create one because it will cause an error

RemoteEvent.OnServerEvent:Connect(function(player, MousePosition)
   local Character = player.Character or player.CharacterAdded:Wait()
   local Humanoid = Character:FindFirstChildOfClass(“Humanoid”)
   local Torso = Character:FindFirstChild(“Torso”) or character:FindFirstChild(“HumanoidRootPart”)

   if Character and Humanoid and Torso and Humanoid.Health > 0 then
      Torso.CFrame = CFrame.new(MousePosition) + Vector3.new(0, 3, 0)
   end
end)

2. UserInputService

--LocalScript in StarterPlayerScripts

local Player = game.Players.LocalPlayer
local UserInputService = game:GetService(“UserInputService”)

local RemoteEvent = game:GetService(“ReplicatedStorage”):WaitForChild(“TeleportClick”) --if you don’t have this RemoteEvent in the ReplicatedStorage, create one because it will cause an error

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
   if gameProcessedEvent then return end

   if (input.UserInputType == Enum.UserInputType.MouseButton1) or (input.UserInputType == Enum.UserInputType.Touch) then
      local MousePosition = UserInputService:GetMouseLocation()
      local camera = workspace.CurrentCamera
      local unitRay = camera:ScreenPointToRay(MousePosition.X, MousePosition.Y)
      RemoteEvent:FireServer(unitRay.Origin)
   end
end)

--Script in ServerScriptService

local RemoteEvent = game:GetService(“ReplicatedStorage”):WaitForChild(“TeleportClick”) --if you don’t have this RemoteEvent in the ReplicatedStorage, create one because it will cause an error

RemoteEvent.OnServerEvent:Connect(function(player, MousePosition)
   local Character = player.Character or player.CharacterAdded:Wait()
   local Humanoid = Character:FindFirstChildOfClass(“Humanoid”)
   local Torso = Character:FindFirstChild(“Torso”) or character:FindFirstChild(“HumanoidRootPart”)

   if Character and Humanoid and Torso and Humanoid.Health > 0 then
      Torso.CFrame = CFrame.new(MousePosition) + Vector3.new(0, 3, 0)
   end
end)

5 comments

0

Perhaps I did not explain this clearly, but what I am trying to accomplish is have the player click on a minimap, a GUI. Which would then teleport them to the corresponding location in the workspace. Refer to my linked posts for what I am trying to do.

0

Oh I understand. I thought you're trying to click in a flat Baseplate and teleporting to the position of the mouse

0

Took me almost an hour to type the answer just to get corrected by the OP. XD

0

My fingers hurt :)\

0

Ahaha, my apologies for not making it clear

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