Advertisement
WeDoScripts

Gui

Feb 3rd, 2019
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.19 KB | None | 0 0
  1. Creator @DutchDeveloper has made a really useful video explaining how [Mad City](https://www.roblox.com/games/1224212277/Mad-City) created their point markers and as he isn't able to post in here yet I'm doing it on his behalf.
  2.  
  3. Video:
  4. https://www.youtube.com/watch?v=JYkiPs6wk4k
  5.  
  6. # Tutorial:
  7. ----
  8. **How the markers work:**
  9. In ReplicatedStorage there's a folder called 'Markers' and in the folder theirs a part, this part is the position of the marker. In each location there's a folder called Config, this holds the information about each marker such as if it's enabled, the icon and the icon colour.
  10. ![image|197x183](upload://tv5R7eXlfK8bZgOt9IbnOrFMUZr.png)
  11.  
  12. In the ScreenGui there is one local script and a frame called 'Holder', the 'Holder' is placed in the centre of the screen and has it provides the distance from each of the sides of the screen (screen size).
  13.  
  14. ![image|273x205](upload://pNocSgUQ9pFilAUiK8Y0COEqBJb.png)
  15.  
  16. The sample is a 0.05 sized circle with a UIAspectRatioConstraint to keep it square. Then, we've got a frame called 'RotateLabel'. This contains the image with the arrow pointing toward the location. As well, it contains the icon of the location that you've set in the 'Config' folder.
  17.  
  18. ![image|226x201](upload://kAIEITR7uP3Q8ELiss1rltQFuPt.png)
  19.  
  20. This goes through the folder 'Markers' and makes a label in the 'Holder' frame and stores the location and label inside a table called 'Markers'.
  21.  
  22. ```LUA
  23. local Markers = {}
  24. local Screen = script.Parent.Holder.AbsoluteSize
  25. local T = game:GetService("TweenService")
  26.  
  27. for i,v in pairs(game.ReplicatedStorage.Markers:GetChildren()) do
  28. local Sample = script.Sample:Clone()
  29. Sample.Parent = script.Parent.Holder
  30. Sample.ImageColor3 = v.Config.IconCollor.Value
  31. Sample.RotateLabel.Arrow.ImageColor3 = v.Config.IconCollor.Value
  32. Sample.Icon.Image = v.Config.Icon.Value
  33. table.insert(Markers,{Sample,v})
  34. end
  35. ```
  36.  
  37. We're using a RenderStepped loop and go through the list of markers. First, we're checking if the marker is enabled, if it isn't it will not render. Then we're calculating where the position is into 2D space. If you look away normally it would still show the marker as it was there. That's why we're checking the Z value of the Vector3 we're receiving. Then we're putting the X, Y and absolute size into the function 'findClosestBorderPoint'. This will clamp the value to the left, right, top or bottom sides so it will not show inside the middle of your screen. If we're facing the marker then we're preventing it from going away from the edge of the screen. As well, we're using the value called 'InViewport' to show the arrow when you're not facing the location.
  38.  
  39. ```LUA
  40. function findClosestBorderPoint(x,y,Absolute)
  41. x = Screen.X - x
  42. y = Screen.Y - y
  43. local distanceToYBorder = math.min(y,Screen.Y-y)
  44. local distanceToXBorder = math.min(x,Screen.X-x)
  45. if distanceToYBorder < distanceToXBorder then
  46. if y < (Screen.Y - y) then
  47. return math.clamp(x,0,Screen.X-Absolute.X),0
  48. else
  49. return math.clamp(x,0,Screen.X-Absolute.X),Screen.Y - Absolute.Y
  50. end
  51. else
  52. if x < (Screen.X - x) then
  53. return 0,math.clamp(y,0,Screen.Y-Absolute.Y)
  54. else
  55. return Screen.X - Absolute.X,math.clamp(y,0,Screen.Y-Absolute.Y)
  56. end
  57. end
  58. end
  59.  
  60. game:GetService("RunService").RenderStepped:Connect(function()
  61. for i,v in pairs(Markers) do
  62. local Marker = v[1]
  63. local Location = v[2]
  64. if not Marker == nil or Location == nil then
  65. table.remove(Markers,i)
  66. end
  67. Marker.Visible = Location.Config.Enabled.Value
  68. if Location.Config.Enabled.Value then
  69. local MarkerPosition, inViewport = workspace.CurrentCamera:WorldToScreenPoint(Location.Position)
  70. local MarkerRotation = workspace.CurrentCamera.CFrame:inverse()*Location.CFrame
  71. local MarkerAbsolute = Marker.AbsoluteSize
  72.  
  73. local MarkerPositionX = MarkerPosition.X - MarkerAbsolute.X/2
  74. local MarkerPositionY = MarkerPosition.Y - MarkerAbsolute.Y/2
  75.  
  76. if MarkerPosition.Z < 0 then
  77. MarkerPositionX,MarkerPositionY = findClosestBorderPoint(MarkerPositionX,MarkerPositionY,MarkerAbsolute)
  78. else
  79. if MarkerPositionX < 0 then
  80. MarkerPositionX = 0
  81. elseif MarkerPositionX > (Screen.X -MarkerAbsolute.X) then
  82. MarkerPositionX = Screen.X - MarkerAbsolute.X
  83. end
  84. if MarkerPositionY < 0 then
  85. MarkerPositionY = 0
  86. elseif MarkerPositionY > (Screen.Y -MarkerAbsolute.Y) then
  87. MarkerPositionY = Screen.Y - MarkerAbsolute.Y
  88. end
  89. end
  90.  
  91. Marker.Position = UDim2.new(0,MarkerPositionX,0,MarkerPositionY)
  92. Marker.RotateLabel.Visible = not inViewport
  93. Marker.RotateLabel.Rotation = 90+math.deg(math.atan2(MarkerRotation.Z, MarkerRotation.X))
  94. end
  95. end
  96. end)
  97. ```
  98.  
  99. That's a basic understanding of how this script works, if you'd like to see anymore please check out the video above.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement