Advertisement
Guest User

Build Message Firer

a guest
Feb 23rd, 2019
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.07 KB | None | 0 0
  1. --scripted by GreekForge
  2. local char = script.Parent
  3. local hum = char:WaitForChild("Humanoid")
  4. local buildComs = game.ReplicatedStorage.Build
  5. local plr = game.Players:WaitForChild(char.Name)
  6. local buildMess = plr.PlayerGui:WaitForChild("BuildMessage")
  7. local mouse = game:GetService("Players").LocalPlayer:GetMouse() --i need to know where he is going to build
  8. local ingame = plr.stats.isInGame
  9. local isBuilding = plr.PlayerGui.FlatUI.BuildMode.LocalScript.Building --from the gui
  10. local camera = workspace.CurrentCamera --so i can store our outline and make it client based
  11. local buildrange = 45
  12. local cost = 5
  13. local wood = plr.stats.ShopValues.Wood
  14. local woodHP = plr.stats.Attributes.WoodHP
  15. local rot = 0;
  16. local canBuild = false; --for the terrainlimitations
  17. local terrainLimit
  18.  
  19. local debounce = true; --why its inverse is beyond me
  20.  
  21. --char:WaitForChild("Humanoid").JumpPower = 10
  22.  
  23. local function canBuildF(oPos, outline)
  24.     if terrainLimit then
  25.         local scorePartN = string.gsub(terrainLimit, "Terrain", "ScorePart")
  26.         local prisonPartN = string.gsub(terrainLimit, "Terrain", "TeamJail")
  27.         local prisonPart = workspace.Arena:FindFirstChild(prisonPartN)
  28.         local scorePart = workspace.Arena:FindFirstChild(scorePartN)
  29.         local dV = (outline.CFrame.upVector * -1).Unit * buildrange --(outline.CFrame.upVector - outline.Position).Unit * buildrange --(maybe)?
  30.         local ray = Ray.new(outline.Position, dV)
  31.         local part = workspace:FindPartOnRay(ray, outline)
  32.         local downTest
  33.         if part then
  34.             downTest = (part.Name == terrainLimit)
  35.         else
  36.             downTest = false
  37.         end
  38.         return ((scorePart.Position - oPos).Magnitude > buildrange and (prisonPart.Position - oPos).Magnitude > buildrange and downTest)
  39.     end
  40. end
  41.  
  42. local function round(val)
  43.     local c1 = math.floor(val)
  44.     local s2 = val - c1
  45.     if math.abs(s2) >= 0.5 then
  46.         return math.ceil(val)
  47.     else
  48.         return c1
  49.     end
  50. end
  51.  
  52. local function grid(oVal, outline)
  53.     local grid = 3.5 --this will need to be set in stone, it can't be dynamic sadly
  54.     local s1 = round(oVal*100)/100 --rounded to the nearest hundredths (0.01)
  55.     local s2 = math.abs(s1)
  56.     if s2 % grid ~= 0 then --just griding it
  57.         s2 = s2 - (s2 % grid)
  58.         if s1 < 0 then --return it back to it's original sign
  59.             return (-1 * s2)
  60.         else --I can't use math.abs sadly
  61.             return s2
  62.         end
  63.     end
  64. end
  65.  
  66. local function checkOutline()
  67.     local outline = camera:FindFirstChild(plr.Name.."_BuildOutline")
  68.     if outline == nil then --set up if missing
  69.         outline = script:WaitForChild("Buildables"):WaitForChild("Basic_Block"):Clone()
  70.         outline.Parent = camera
  71.         outline.Name = plr.Name.."_BuildOutline"
  72.         outline.Transparency = 1
  73.         outline.Anchored = true
  74.         outline.CanCollide = false
  75.         outline.BrickColor = BrickColor.new("Really blue")
  76.         outline.Material = Enum.Material.SmoothPlastic
  77.     end
  78. end
  79.  
  80. local function blockOutline(outline)
  81.     local ray = Ray.new(camera.CFrame.p, mouse.UnitRay.Direction*(buildrange*2))
  82.     local part, rPosition = workspace:FindPartOnRay(ray, outline) --(hit) and (position of where the ray hit)
  83.     local nPosition = Vector3.new(grid(rPosition.X, outline), rPosition.Y, grid(rPosition.Z, outline))
  84.     if part then
  85.         terrainLimit = tostring(plr.Team).."Terrain" --example: GreenTerrain
  86.         local check = (rPosition - char.HumanoidRootPart.Position).Magnitude < buildrange
  87.         if part.Name == terrainLimit and (wood.Value > cost)and canBuildF(rPosition, outline) and check then
  88.             outline.Transparency = 0.8
  89.             local posChange = nPosition + Vector3.new(0,(outline.Size.y/2),0)
  90.             outline.Position = posChange
  91.             outline.CFrame = CFrame.new(outline.Position) * CFrame.Angles(0, math.rad(rot), 0) --cframe wants rad
  92.             outline.BrickColor = BrickColor.new("Really blue") --meaning you can build
  93.             canBuild = true --for the other functions
  94.             --
  95.         elseif part:FindFirstChild("Team") ~= nil and check then
  96.             if part.Team.Value == terrainLimit  then
  97.                 outline.Transparency = 0.5
  98.                 local posChange = part.Position + Vector3.new(0,(part.Size.y/2 + outline.Size.y/2),0)
  99.                 outline.Position = posChange
  100.                 outline.CFrame = CFrame.new(outline.Position) * CFrame.Angles(0, math.rad(rot), 0) --cframe wants rad
  101.                 outline.BrickColor = BrickColor.new("Really blue") --meaning you can build
  102.                 canBuild = true --for the other functions
  103.             end
  104.         else
  105.             outline.Transparency = 0.25
  106.             outline.Position = nPosition + Vector3.new(0,(outline.Size.y/2),0)
  107.             outline.CFrame = CFrame.new(outline.Position) * CFrame.Angles(0, math.rad(rot), 0) --cframe wants rad
  108.             outline.BrickColor = BrickColor.new("Really red") --meaning you can't build
  109.             canBuild = false
  110.         end
  111.        
  112.     end
  113. end
  114.  
  115. game:GetService("UserInputService").InputEnded:Connect(function(key, gpe)
  116.     if not gpe and key.KeyCode == Enum.KeyCode.R then --rotational magic
  117.         rot = rot + 90
  118.         if rot >= 360 then
  119.             rot = rot - 360
  120.         end
  121.     end
  122. end)
  123.  
  124. mouse.Button1Up:Connect(function() --activates when clicked
  125.     local outline = camera:FindFirstChild(plr.Name.."_BuildOutline")
  126.     if ingame.Value == true and isBuilding.Value == true and outline then
  127.         if canBuild and terrainLimit and (wood.Value > cost) and debounce then --should be checking for wood as well
  128.             local sPos = outline.Position --send position
  129.             local pass = "Building" --Password/what it needs to do
  130.             local id = 1 --this will not change until we have more block types
  131.             debounce = false
  132.             --print("Click!")
  133.             buildComs:FireServer(pass, sPos, rot, id, woodHP.Value, terrainLimit) --pass pos rot id woodHP, terrainLimit(gotta remember)
  134.         elseif not canBuild then
  135.             print("Sending message that can't build")
  136.             buildMess:Fire("You cannot build in that location, red outline shows this.")
  137.             print("Message fired!")
  138.         end
  139.     end
  140. end)
  141.  
  142. local function datLoop()
  143.     while wait() do
  144.         checkOutline()
  145.         local outline = camera:FindFirstChild(plr.Name.."_BuildOutline")
  146.         if isBuilding.Value == true and outline then
  147.             blockOutline(outline)
  148.         elseif outline then
  149.             outline.Transparency = 1
  150.         else
  151.             warn("Player is missing their outline!")
  152.         end
  153.         if not debounce then
  154.             wait(0.1)
  155.             debounce = true
  156.         end
  157.     end
  158. end
  159.  
  160. coroutine.resume(coroutine.create(datLoop)) --coroutine time
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement