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

:crossed_fingers: I sure hope it does. If it doesn’t, lmk if there are any errors and where the part does spawn. I might have made a simple math error that can be easily fixed.

1 Like

For some reason… It didn’t work at all. Here’s a picture:

The part is at the blue selectionbox, I selected it. Here’s my code now:

local GPS_BUTTON = script.Parent;

local player  = game:GetService("Players").LocalPlayer
local Mouse = player:GetMouse()

local location = script.Parent.Parent:WaitForChild("Location")

local UserInputService = game:GetService("UserInputService")

local char = player.Character or player.CharacterAdded:Wait()

local humanoid = char:WaitForChild("Humanoid")

local locations = {};

local mapXSize = 508
local mapSizeZ = 509.5

local mapXAnchor = -251.5 --number (the x position where the top left of the map starts)
local mapZAnchor = 252.67 --number (the z position where the top left of the map starts)

humanoid.Touched:Connect(function(hit)
    if hit.Name == "GPS" then
        hit:Destroy()

        for index, location in ipairs(locations) do
            location:Destroy()
        end

        locations = {}
    end
end)

GPS_BUTTON.MouseButton1Click:Connect(function()
    if game.Workspace:FindFirstChild("GPS") then return end

    local minX, minY = GPS_BUTTON.AbsolutePosition.X, GPS_BUTTON.AbsolutePosition.Y

    local maxX, maxY = minX + GPS_BUTTON.AbsoluteSize.X, minY + GPS_BUTTON.AbsoluteSize.Y

    local mouseX = math.clamp(UserInputService:GetMouseLocation().X, minX, maxX)

    local mouseY = math.clamp(UserInputService:GetMouseLocation().Y, minY, maxY)

    local xPixel, yPixel = (maxX - minX), (maxY - minY)

    local xPos = (mouseX - minX) / xPixel

    local yPos = (mouseY - minY) / xPixel

    local scale = UDim2.new(xPos, 0, yPos, 0)


    local newLo = location:Clone()

    table.insert(locations, newLo)

    newLo.Visible = true

    newLo.Position = scale

    newLo.Parent = location.Parent

    local anotherLo = newLo:Clone()

    anotherLo.Visible = true

    anotherLo.Size = UDim2.new(0.116, 0,0.111, 0)

    anotherLo.Position = scale

    anotherLo.Parent = GPS_BUTTON.Parent.Parent.Frame.Map

    table.insert(locations, anotherLo)
    
    local mapX = (mapXSize/ xPos) + mapXAnchor --take the maps size, divide by where they clicked, and add that to the anchor point
    local mapZ = mapZAnchor - (mapXSize/ xPos)
    
    local pos = Vector3.new(mapX, 10, mapZ)
    
    local PART = Instance.new("Part")

    PART.Size = PART.Size + Vector3.new(0, 20, 0)
    
    PART.Position = pos

    PART.Anchored = true

    PART.Parent = game.Workspace

    PART.CanCollide = false

    PART.Transparency = 0.5

    PART.Name = "GPS";
end)
1 Like

I think I know they problem, gimme a minute to do some calculations.

Okay. Thank you very much, I appreciate it. :slight_smile:

1 Like

Would you mind printing some things below this line and telling me what they print. Also if you could provide another screen shot of where the part spawns with these prints that would be great!

local pos = Vector3.new(mapX, 10, mapZ) --below this print:

print(pos)
print(xPos, yPos)

Ok, give me a minute or two. I’m doing that.

The part is the blue selection box.

print(pos) --  773.401855, 10, -1637.50085
print(xPos, yPos) 0.49565722651386, 0.26955234857923
1 Like

I have a feeling that the reason it isn’t working is that I am adding the opposite of how I should. Real quick could you find the X and Z values of the bottom right corner for me pls.

Edit: Another problem is that the xPos and the yPos is getting the position out of the entire frame, which includes the green part. For it to work we need to get the x and y position of only the GUI map image.

Here:

X = 250.925
Z = --251.632

The problem here is that you are getting the mouses position relative to the entire screen. We need to get the mouses position relative the the Map Image. I have to go but I can help you later.

I gtg sleep soon. But you can add me on discord and Roblox. We can talk about this when I wake up. Discord: Chain#5435

1 Like

I still haven’t figured it out, I tried everything I could! :frowning: