Right now I'm working on an FPS game and am experiencing large amounts of lag when firing an automatic gun. I'm guessing it has something to do with repeatedly invoking the remote function 'damageplayer' in the function 'FireRay' each time it goes through the loop while the player is holding down the mouse. Does anyone know if there is a better way of doing this other than repeatedly invoking the remote function?

Solution: I changed the remote function to a remote event and it eliminated the lag. Thanks!


function FireRay(mod,startpos,endpos)
	local firesound = modules[mod.name].FireSound:Clone()
	firesound.Parent = head
	firesound:Play()
	local ray = Ray.new(startpos.p, (endpos - startpos.p) * 300) 
	local part, position = workspace:FindPartOnRayWithIgnoreList(ray,{character})
	damageplayer:InvokeServer(mod,part,position)
end

function Fire()
	local curammo = backpack[val_equipped.Value].CurAmmo
	if CanFire and not Running and viewmodel then
		local weapon = viewmodel[mod.name]
		local barrel = weapon.Barrel
		if mod.auto then
			Holding = true
			while Holding and curammo.Value > 0 do
				CanFire = false
				MuzzleFlash()
				fireanim:Play()
				FireRay(mod,barrel.CFrame,mouse.Hit.p)
				curammo.Value = curammo.Value - 1
				Recoil()
				wait(mod.waittime * server_mul)
				CanFire = true
			end
		elseif curammo.Value > 0 then
			CanFire = false
			MuzzleFlash()
			fireanim:Play()
			FireRay(mod,barrel.CFrame,mouse.Hit.p)
			curammo.Value = curammo.Value - 1
			Recoil()
			wait(mod.waittime * server_mul)
			CanFire = true
		end
		if curammo.Value == 0 then
			Reload()
		end
	else
		Running = false
	end
end

3 comments

0

InvokeServer will yield the thread until a response is returned from the server. If you try it in Play Solo mode, it should be faster.

0

use FireServer

0

I’ll try using a remote event instead of a remote function and see what happens. Thanks!