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

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

Asked by 2 years ago

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

Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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)
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. SuperLittleAdmin 257 — 2y
0
Oh I understand. I thought you're trying to click in a flat Baseplate and teleporting to the position of the mouse T3_MasterGamer 2189 — 2y
0
Took me almost an hour to type the answer just to get corrected by the OP. XD T3_MasterGamer 2189 — 2y
0
My fingers hurt :)\ T3_MasterGamer 2189 — 2y
0
Ahaha, my apologies for not making it clear SuperLittleAdmin 257 — 2y
Ad

Answer this question