Advertisement
Er1x_Official

server side projectile raycast system

Dec 10th, 2022
1,901
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.05 KB | None | 0 0
  1. local Debris = game:GetService("Debris")
  2. local TweenService = game:GetService("TweenService")
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4.  
  5. local Module = require(script.FastCastRedux)
  6.  
  7. local Tool = script.Parent
  8. local RemoteEvent = Tool:WaitForChild("RemoteEvent")
  9. local GunConfig = Tool:WaitForChild("Configuration")
  10.  
  11. local Bullet = ReplicatedStorage:FindFirstChild("Bullet")
  12. local Audio = Tool.Handle:FindFirstChild("Shoot")
  13.  
  14. local OnCooldown = false
  15.  
  16. function FolderStorage()
  17.     local Folder = workspace:FindFirstChild("BulletStorage")
  18.     if not Folder then
  19.         Folder = Instance.new("Folder",workspace)
  20.         Folder.Name = "BulletStorage"
  21.     end
  22.    
  23.     return Folder
  24. end
  25.  
  26. function Hit(Cast,Result)
  27.     local Hit = Result.Instance
  28.     if Hit then
  29.         local Humanoid = Hit.Parent:FindFirstChildOfClass("Humanoid") or Hit.Parent.Parent:FindFirstChildOfClass("Humanoid")
  30.         if Humanoid then
  31.             Humanoid:TakeDamage(GunConfig.Damage.Value)
  32.         end
  33.     end
  34. end
  35.  
  36. function RayUpdate(Cast, Origin, Direction, Length, Velocity, PartObject)
  37.     if PartObject then
  38.         local PartLength = PartObject.Size.Z/2
  39.         local BaseCF = CFrame.new(Origin,Origin+Direction)
  40.        
  41.         TweenService:Create(PartObject,TweenInfo.new(((Origin-Direction).Magnitude/Velocity.Magnitude)/1.2),{CFrame = BaseCF*CFrame.new(0,0,-(Length-PartLength))}):Play()
  42.     end
  43. end
  44.  
  45. function RayTerminating(Cast)
  46.     local RayInfo = Cast.RayInfo
  47.     if RayInfo then
  48.         local PartObject = RayInfo.CosmeticBulletObject
  49.         if PartObject then
  50.             TweenService:Create(PartObject,TweenInfo.new(.5),{Transparency = 1}):Play()
  51.             Debris:AddItem(PartObject,.55)
  52.         end
  53.     end
  54. end
  55.  
  56. RemoteEvent.OnServerEvent:Connect(function(Player,Position)
  57.     if Position ~= nil and Player.Character and not OnCooldown then
  58.         OnCooldown = true
  59.         task.delay(GunConfig.ShootSpeed.Value,function()
  60.             OnCooldown = false
  61.         end)
  62.        
  63.         local FastCastRedux = Module.new()
  64.         FastCastRedux.VisualizeCasts = false
  65.        
  66.         local Params = RaycastParams.new()
  67.         Params.FilterType = Enum.RaycastFilterType.Blacklist
  68.         Params.FilterDescendantsInstances = {Player.Character}
  69.         Params.IgnoreWater = true
  70.        
  71.         local Behavior = Module.newBehavior()
  72.        
  73.         Behavior.RaycastParams = Params
  74.         Behavior.MaxDistance = 3000
  75.         Behavior.AutoIgnoreContainer = false
  76.         Behavior.CosmeticBulletTemplate = Bullet
  77.         Behavior.CosmeticBulletContainer = FolderStorage()
  78.         Behavior.Acceleration = Vector3.new(0,GunConfig.BulletDrop.Value,0)
  79.         Behavior.HighFidelityBehavior = Module.HighFidelityBehavior.Default
  80.        
  81.         local Origin = Tool.Handle.ShootPart.WorldPosition
  82.         local Direction = CFrame.new(Origin)*CFrame.new(0,0,-100)
  83.         local OriginToDirection = CFrame.new(Origin,Position).LookVector
  84.        
  85.         FastCastRedux:Fire(Origin,Direction.Position,OriginToDirection*GunConfig.Velocity.Value,Behavior)
  86.        
  87.         if Audio then
  88.             local SFX = Audio:Clone()
  89.             SFX.PlayOnRemove = true
  90.             SFX.Parent = Audio.Parent
  91.             SFX:Destroy()
  92.         end
  93.        
  94.         FastCastRedux.RayHit:Connect(Hit)
  95.         FastCastRedux.LengthChanged:Connect(RayUpdate)
  96.         FastCastRedux.CastTerminating:Connect(RayTerminating)
  97.     end
  98. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement