Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local RunService = game:GetService("RunService")
- local Workspace = game:GetService("Workspace")
- local Modules = require(ReplicatedStorage:WaitForChild("Modules"))
- local Network = Modules.Get("Network")
- local GunDatabase = Modules.Get("GunDatabase")
- local Module = {}
- if RunService:IsServer() then
- local PlayerGunUseCache = {}
- local function CanFire(Player)
- local LastFired = PlayerGunUseCache[Player]
- if not LastFired then
- PlayerGunUseCache[Player] = os.clock()
- return true
- elseif os.clock()-LastFired > GunDatabase.Gun.ReloadTime then
- PlayerGunUseCache[Player] = os.clock()
- return true
- end
- end
- Network.ListenTo("FireGun", function(Player, Origin, Direction)
- if not CanFire(Player) then return end
- for _, OtherPlayer in ipairs(Players:GetPlayers()) do
- if OtherPlayer == Player then continue end
- Network.FireTo("Client", "ReplicateBullet", OtherPlayer, Origin, Direction)
- end
- end)
- else--Is client
- local Heartbeat = RunService.Heartbeat
- local BulletsFolder = Workspace:WaitForChild("Bullets")
- local Bullet = script:WaitForChild("Bullet")
- local BulletDatabase = GunDatabase.Bullet
- local BulletSpeed = BulletDatabase.Speed
- local BulletGravity = BulletDatabase.Gravity
- local AirFriction = BulletDatabase.AirFriction
- local BulletFriction = BulletDatabase.Friction
- local BulletList = {}--[1] = {}
- local RayParams = RaycastParams.new()
- RayParams.FilterType = Enum.RaycastFilterType.Blacklist
- RayParams.FilterDescendantsInstances = {BulletsFolder, Workspace.Entities}
- function Module.CreateBullet(Position, Direction)
- local NewBullet = Bullet:Clone()
- NewBullet.Position = Position
- NewBullet.Position = Position
- NewBullet.Parent = BulletsFolder
- table.insert(BulletList, {
- Model = NewBullet,
- TimeLeft = BulletDatabase.LifeTime,
- Velocity = Direction * BulletDatabase.Speed,
- Position = Position
- })
- end
- --Network.ListenTo("ReplicateBullet", function(OriginCFrame, BulletDirection)
- -- table.insert(BulletList, {
- -- })
- --end)
- Network.ListenTo("ReplicateBullet", Module.CreateBullet)
- Heartbeat:Connect(function(TimeElapsed)
- for i = #BulletList, 1, -1 do
- local BulletData = BulletList[i]
- local Model = BulletData.Model
- local Velocity = BulletData.Velocity
- local TimeLeft = BulletData.TimeLeft
- TimeLeft -= TimeElapsed
- if TimeLeft < 0 or Velocity.Magnitude <= .5 then
- Model:Destroy()
- table.remove(BulletList, i)
- continue
- end
- local CurrentPos = Model.Position
- local NewVelocity = Velocity + BulletGravity * TimeElapsed
- local Displacement = NewVelocity * TimeElapsed
- local NewPos = CurrentPos + NewVelocity * TimeElapsed
- local Results = Workspace:Raycast(NewPos - (Displacement.Unit * 0.05), Displacement, RayParams)
- if Results then
- local Normal = Results.Normal
- local Dot = NewVelocity:Dot(Normal)
- local ReflectedNormal = -Normal * Dot * 2
- local BulletMass = Bullet:GetMass()
- local CollideeMass = Results.Instance:GetMass()
- local TotalMass = BulletMass + CollideeMass
- local CollisionRatio = CollideeMass/TotalMass
- BulletData.Velocity = (NewVelocity + ReflectedNormal) * (1-BulletFriction*CollisionRatio)
- BulletData.Position = Results.Position-- Set to collision Point
- local Sound = script.Hit:Clone()
- Sound.Parent = Model
- Sound:Play()
- game.Debris:AddItem(Sound, Sound.TimeLength)
- else
- BulletData.Position = NewPos
- BulletData.Velocity = NewVelocity
- end
- Model.Position = BulletData.Position
- BulletData.TimeLeft = TimeLeft
- end
- end)
- end
- return Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement