Er1x_Official

raycast gun script

Oct 31st, 2022 (edited)
3,158
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.05 KB | Gaming | 1 0
  1. local TweenService = game:GetService("TweenService")
  2. local Debris = game:GetService("Debris")
  3.  
  4. local Tool:Tool = script.Parent
  5. local Handle = Tool:FindFirstChild("Handle")
  6. local ShootPart = Handle:FindFirstChild("Attachment")
  7. local RemoteEvent = Tool:FindFirstChild("RemoteEvent")
  8. local OnCooldown = false
  9.  
  10. local Do = {
  11.     Damage = 30;
  12.     Cooldown = 0.5;
  13.     Visualize = true;
  14.     ShootSound = "rbxassetid://5238024665";
  15. }
  16.  
  17. RemoteEvent.OnServerEvent:Connect(function(Player,Received)
  18.     if not OnCooldown then
  19.         OnCooldown = true
  20.         task.delay(Do.Cooldown,function()
  21.             OnCooldown = false
  22.         end)
  23.        
  24.         local Origin = ShootPart.WorldPosition
  25.         local Direction = (Received.Position-Origin).Unit*3000
  26.         local Raycast = workspace:Raycast(Origin,Direction)
  27.        
  28.         local Intersection = Raycast and Raycast.Position or Origin + Direction
  29.         local Distance = (Origin - Intersection).Magnitude
  30.  
  31.         local Visualizer = Instance.new("Part")
  32.         Visualizer.CanTouch = false
  33.         Visualizer.CanCollide = false
  34.         Visualizer.CanQuery = false
  35.         Visualizer.CastShadow = false
  36.         Visualizer.Anchored = true
  37.         Visualizer.Material = Enum.Material.Neon
  38.         Visualizer.Color = Color3.fromRGB(255, 255, 0)
  39.         Visualizer.Size = Vector3.new(.15,.15,Distance)
  40.         Visualizer.CFrame = CFrame.new(Origin, Intersection)*CFrame.new(0,0,-Distance/2)
  41.  
  42.         if Do.Visualize == true then
  43.             Visualizer.Parent = workspace
  44.             TweenService:Create(Visualizer,TweenInfo.new(.3),{Transparency = 1, Size = Vector3.new(0,0,Distance)}):Play()
  45.             Debris:AddItem(Visualizer,.35)
  46.         end
  47.  
  48.         if (Do.ShootSound ~= "" or Do.ShootSound ~= nil) then
  49.             local Sound = Instance.new("Sound")
  50.             Sound.SoundId = Do.ShootSound
  51.             Sound.Volume = .65
  52.             Sound.PlayOnRemove = true
  53.             Sound.Parent = Handle
  54.             Sound:Destroy()
  55.         end
  56.        
  57.         if Raycast then
  58.             local Hit:Part = Raycast.Instance
  59.             local Humanoid = (Hit.Parent:FindFirstChildOfClass("Humanoid") or Hit.Parent.Parent:FindFirstChildOfClass("Humanoid"))
  60.  
  61.             if Humanoid and Humanoid.Parent ~= Player.Character then
  62.                 Humanoid:TakeDamage(Do.Damage)
  63.             end
  64.         end
  65.     end
  66. end)
Advertisement
Add Comment
Please, Sign In to add comment