Advertisement
HowToRoblox

DamageIndicatorHandler

Jan 17th, 2023
2,466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.91 KB | None | 0 0
  1. local gui = script.Parent
  2. local indicator = script:WaitForChild("DamageIndicator")
  3.  
  4. local remote = game:GetService("ReplicatedStorage"):WaitForChild("DamageIndicatorReplicatedStorage"):WaitForChild("DamageEvent")
  5.  
  6. local tweenService = game:GetService("TweenService")
  7. local indicatorFadeTI = TweenInfo.new(0.3, Enum.EasingStyle.Quad, Enum.EasingDirection.In)
  8.  
  9. local camera = workspace.CurrentCamera
  10.  
  11. local timeAfterIndicatorExpires = 5
  12.  
  13.  
  14.  
  15. function getCentreCF(object:Instance)
  16.    
  17.     local instanceCF = object.CFrame
  18.    
  19.     local pos = instanceCF.Position
  20.     local lookAt = instanceCF.LookVector * Vector3.new(1, 0, 1)
  21.    
  22.     local centreCF = CFrame.new(pos, pos + (lookAt))
  23.    
  24.     return centreCF
  25. end
  26.  
  27. function calculateAngle(centre:CFrame, point:Vector3)
  28.    
  29.     local damageDir = centre:PointToObjectSpace(point)
  30.  
  31.     local theta = math.atan2(damageDir.Z, damageDir.X)
  32.     local angleInDegrees = math.deg(theta) + 90
  33.    
  34.     return angleInDegrees
  35. end
  36.  
  37. function damageDealt(originOfDamage:Vector3)
  38.    
  39.     local centreCF = getCentreCF(camera)
  40.     local angleInDegrees = calculateAngle(centreCF, originOfDamage)
  41.    
  42.     local newIndicator = indicator:Clone()
  43.     newIndicator.DamageIndicatorImage.ImageTransparency = 0
  44.     newIndicator.Visible = true
  45.    
  46.     newIndicator.Rotation = angleInDegrees
  47.    
  48.     newIndicator.Parent = gui
  49.    
  50.    
  51.     task.spawn(function()
  52.         while newIndicator and newIndicator.Parent == gui do
  53.            
  54.             local centreCF = getCentreCF(camera)
  55.             local angleInDegrees = calculateAngle(centreCF, originOfDamage)
  56.            
  57.             newIndicator.Rotation = angleInDegrees
  58.            
  59.             game:GetService("RunService").Heartbeat:Wait()
  60.         end
  61.     end)
  62.    
  63.    
  64.     task.wait(timeAfterIndicatorExpires)
  65.    
  66.     local indicatorFadeTween = tweenService:Create(newIndicator.DamageIndicatorImage, indicatorFadeTI, {ImageTransparency = 1})
  67.        
  68.     indicatorFadeTween:Play()
  69.     indicatorFadeTween.Completed:Wait()
  70.        
  71.     newIndicator:Destroy()
  72. end
  73.  
  74.  
  75. remote.OnClientEvent:Connect(damageDealt)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement