Hello.
I’m trying to make a teleport, in that if you move your mouse out of the range of the teleport, it shows a GUI that says “Out of Range”. If your within the range, it will say, “Within Range”. How will I do this?
Note: I am not asking for a script, but a way as to how to achieve this. Thank you.
2 Likes
First you can get the mouse’s position in 3D space using Mouse | Roblox Creator Documentation . This returns the CFrame of where the mouse points at. Then, call Player | Roblox Creator Documentation to measure the distance between your character and the location youre pointing at. If this distance is out of range, then the GUI should show “Out of Range”, and if its in range it should show “Within Range”. Put this code inside a loop so it runs as you move your mouse.
local screenGui = script:FindFirstAncestorOfClass("ScreenGui")
local frame = script.Parent
local players = game:GetService("Players")
local player = players.LocalPlayer or players.PlayerAdded:Wait()
local mouse = player:GetMouse()
mouse.Move:Connect(function()
local xDimension = mouse.X
local yDimension = mouse.Y
local xResolution = screenGui.AbsoluteSize.X
local yResolution = screenGui.AbsoluteSize.Y
local framePosXStart = math.round(xResolution * frame.Position.X.Scale) + frame.Position.X.Offset
local framePosXEnd = framePosXStart + math.round(xResolution * frame.Size.X.Scale) + frame.Size.X.Offset
local framePosXMid = math.round((framePosXEnd - framePosXStart)/2)
local framePosYStart = math.round(yResolution * frame.Position.Y.Scale) + frame.Position.Y.Offset
local framePosYEnd = framePosYStart + math.round(xResolution * frame.Size.Y.Scale) + frame.Size.Y.Offset
local framePosYMid = math.round((framePosYEnd - framePosYStart)/2)
frame.Position = UDim2.new(0, xDimension-framePosXMid, 0, yDimension-framePosYMid)
end)
This doesn’t just make the UI instance follow the mouse using the top left corner but instead the center of the UI instance instead.
Demonstration:
https://gyazo.com/7f7ee6328cb746c6f08ba3198629f682
If you’d prefer the UI instance to follow the mouse using one of its 4 corners then let me know.
3 Likes