Advertisement
DrawingJhon

Placement System

Apr 17th, 2021 (edited)
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  1. local player = game:GetService("Players").LocalPlayer
  2. local mouse = player:GetMouse()
  3. local TweenService = game:GetService("TweenService")
  4.  
  5. local block = Instance.new("Part", workspace)
  6. block.Anchored = true
  7. block.Size = Vector3.new(4, 4, 4)
  8. block.TopSurface = "Smooth"
  9. block.BottomSurface = "Smooth"
  10. block.BrickColor = BrickColor.new("Bright green")
  11. block.Material = "Grass"
  12. block.CanCollide = false
  13. block.Transparency = 0.5
  14.  
  15. function round(pos, offset)
  16.     local result = {}
  17.     for i, val in pairs{X=pos.X;Y=pos.Y;Z=pos.Z} do
  18.         local def = val - (val%offset)
  19.         result[i] = def
  20.     end
  21.     local finalPos = Vector3.new(result.X, result.Y, result.Z)
  22.     return finalPos + (Vector3.new(offset,offset,offset)/2)
  23. end
  24.  
  25. local lastPos = Vector3.new()
  26. local tween
  27.  
  28. local offset = 4
  29. local REACH = 100
  30.  
  31. while wait() do
  32.     local unitRay = mouse.UnitRay
  33.  
  34.     local castParams = RaycastParams.new()
  35.     castParams.FilterDescendantsInstances = { player.Character , block}
  36.  
  37.     -- automatically ignores the player's character
  38.    
  39.     local result = Workspace:Raycast(
  40.         unitRay.Origin, unitRay.Direction * REACH,
  41.         castParams
  42.     )
  43.  
  44.     if result then
  45.         local newPos = round(result.Position + result.Normal * 1.5, offset)
  46.         if (lastPos-newPos).magnitude > .5 and tween then
  47.             tween:Cancel()
  48.         end
  49.         lastPos = newPos
  50.         tween = TweenService:Create(block, TweenInfo.new(0.1), {Position = newPos})
  51.         tween:Play()
  52.     end
  53. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement