M4A1 gun is "delayed" when damaging other humanoids

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)
3 Likes
humanoid.Health -= Damage

do Humanoid:TakeDamage()

4 Likes

Thanks, but the issue is also still persistent tho

4 Likes

could be

	for i, particles in ipairs(particles) do
		particles.Enabled = true
		wait(0.15) --halting the whole script since its a server script
		particles.Enabled = false
	end
5 Likes

It was that actually, but i still need to have some visual effects :sob:, what could i do to substitute it or similar

2 Likes

well you have the viewModel locally itd be the same how youd do it in the server script right?

3 Likes

What do you mean, didnt get you there

where do you have your particles stored? im not seeing it declared in any of the scripts

1 Like

the particles are inside ‘MuzzleFlashAttach’ which is inside the Barrel of the gun

local particles = gun.Barrel.MuzzleFlashAttach:GetChildren()
1 Like

do you have gun declared in the local script?

1 Like

Yea the whole
local gun = script.parent
declares it

2 Likes

You can either do this:

task.spawn(function() -- spawns a task, this means that while this is running, the next line stated after this function is immediately ran.
  for i, particles in ipairs(particles) do
    particles.Enabled = true
    task.wait(0.15) -- task.wait() is recommended over wait().
    particles.Enabled = false
  end
end)

or this:

for i, particles in ipairs(particles) do
    particles:Emit()
    task.wait(0.15) -- task.wait() is recommended over wait().
  end
1 Like

Alright thank you, ill try this

Right yeah, I’d like to reiterate; task.spawn is useful for many things, what it does is it “spawns” a function. This means that, in the same script, 2 functions can simultaneously be ran regardless of if they have any task.wait()s in em. ParticleEmitters have a function called Emit(), which activates it for a short period of time.

2 Likes

i got you !! thank you for telling me this, will be very helpful for future scripts

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.