Currently, I have been making a really well off system; with an M1911 done and otherwise, but when it came to making my M4A1 which has a few script edits; the only issue is that unlike the M1911, it seems to be feel delayed when damaging other humanoids.
(Like, The M4A1 shoots someone, and the damage doesn’t come until like a second afterwards.)
Hopefully this can visualize what I mean.
https://i.gyazo.com/aaa934d3027840c3de592416eb4fac73.mp4
https://i.gyazo.com/1527ef86fac883bba469fac30abc10b8.mp4
I doubt this is an issue with my server script, but the local script is here.
--local script
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")
local uis = game:GetService("UserInputService")
local runService = game:GetService("RunService")
local gun = script.Parent
local gunAmmo = gun:GetAttribute("AmmoCount")
local gunAmmoSet = 31
local Looping = false
local canReload = false
local canShoot = true
local fireGunEvent = replicatedStorage.FireGun
local reloadingSFXevent = replicatedStorage.ReloadingSFX
local gunDebounce = false
local debounceTime = gun:GetAttribute("Cooldown")
local reloadTime = gun:GetAttribute("ReloadTime")
local player = players.LocalPlayer
local mouse = player:GetMouse()
local cam = workspace.CurrentCamera
------------------------------------------------------------------------------------------------------
local recoil = math.rad(1.5)
local rvSpeed = 4
local maxRv = 1/4
maxRv = -1-1/(maxRv-1)
local offs = 0
local pOffs = 0
------------------------------------------------------------------------------------------------------
runService.RenderStepped:Connect(function(dt)
local speed = rvSpeed*dt
cam.CFrame *= CFrame.Angles(offs, 0, 0)
offs = (1 - speed*maxRv) * (pOffs - offs*speed)
pOffs = offs
end)
local function applyRecoil()
pOffs = 0 -- so it stops compensating for the previous shot
offs = recoil
end
gun.Equipped:Connect(function()
local EquipAnim = player.Character.Humanoid:LoadAnimation(gun.Animations.Equip)
local ReloadAnim = player.Character.Humanoid:LoadAnimation(gun.Animations.Reload)
local AimAnim = player.Character.Humanoid:LoadAnimation(gun.Animations.Aiming)
local ShootAnim = player.Character.Humanoid:LoadAnimation(gun.Animations.Shooting)
canShoot = false
canReload = false
EquipAnim:Play()
wait(2.23)
if gun.Parent == player.Character then
AimAnim:Play()
canShoot = true
canReload = false
end
uis.InputBegan:Connect(function(input, _GameProccessed)
if _GameProccessed then return end
if input.KeyCode == Enum.KeyCode.R and canReload == true then
canReload = false
ReloadAnim:Play()
gunAmmo = gunAmmoSet
print("reloading!")
reloadingSFXevent:FireServer(gun)
canShoot = false
wait(reloadTime)
canShoot = true
end
end)
while wait() do
if uis:IsMouseButtonPressed(Enum.UserInputType.MouseButton1) then
if not gunDebounce and gunAmmo >= 0 and canShoot == true then
print(gunAmmo)
ShootAnim:Play()
applyRecoil()
gunAmmo -= 1
gunDebounce = true
canReload = true
local startPos = gun.Barrel.MuzzleFlashAttach.WorldPosition
local endPos = mouse.Hit.Position
print("gun fired")
fireGunEvent:FireServer(gun, startPos, endPos)
wait(debounceTime)
gunDebounce = false
end
end
end
end)
gun.Unequipped:Connect(function()
canReload = false
canShoot = false
if canReload == false then
print("gun cannot be reloaded")
end
ShootAnim:Stop()
ReloadAnim:Stop()
AimAnim:Stop()
EquipAnim:Stop()
end)
and
--Server Script
local replicatedStorage = game:GetService("ReplicatedStorage")
local fireGunEvent = replicatedStorage.FireGun
local reloadingSFXevent = replicatedStorage.ReloadingSFX
fireGunEvent.OnServerEvent:Connect(function(player, gun, startPos, endPos)
local gunshotSFX = gun.Handle.GunShot
local reloadSFX = gun.Handle.Reload
local particles = gun.Barrel.MuzzleFlashAttach:GetChildren()
local MaxDistance = gun:GetAttribute("MaxDistance")
local Damage = gun:GetAttribute("Damage")
local direction = (endPos - startPos).Unit * MaxDistance
local raycastRes = game.Workspace:Raycast(startPos, direction)
gunshotSFX:Play()
for i, particles in ipairs(particles) do
particles.Enabled = true
wait(0.15)
particles.Enabled = false
end
if raycastRes then
print(raycastRes)
local hitChar = raycastRes.Instance.Parent
local humanoid = hitChar:FindFirstChild("Humanoid")
if humanoid then
if hitChar.Name ~= player.Character.Name then
humanoid.Health -= Damage
end
end
end
end)
reloadingSFXevent.OnServerEvent:Connect(function(player, gun)
local reloadSFX = gun.Handle.Reload
reloadSFX:Play()
end)