Hi! I’m making a system where if you click anywhere on the minimap, there will be a part created in the world at that place, I’m close to finishing it, but sometimes it doesn’t position the in-world part properly, sometimes it does, and sometimes not, here is a picture of how it works:
Here’s my code:
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 = {};
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 Raycast = game.Workspace.CurrentCamera:ScreenPointToRay(newLo.AbsolutePosition.X, newLo.AbsolutePosition.Y, 0)
local Ray = Ray.new(Raycast.Origin, Raycast.Direction * 100)
local hit, pos = game.Workspace:FindPartOnRayWithIgnoreList(Ray, {char})
local PART = Instance.new("Part")
PART.Size = PART.Size + Vector3.new(0, 20, 0)
pos = Vector3.new(pos.X, 10 , pos.Z)
PART.Position = pos
PART.Anchored = true
PART.Parent = game.Workspace
PART.CanCollide = false
PART.Transparency = 0.5
PART.Name = "GPS";
end)
I know I have made a post about this before, but I didn’t get any help, I tried to search but no luck at all.
Any help that solves my problem will save my day, Thanks.