Help with prediction in primary attack hook

Hi, I have been working on a swep base for some time, and it burdens me to come here, but when I fire my weapon, it almost always fails to hit a moving target, I was wondering why.

as you can see bellow, I have a simple function that does some stuff at the top and then does a for loop to fire shotgun bullets.

any help would be greatly appreciated.

FireTacoBullet - fires custom bullet function, with penetration etc.
function SWEP:FireTacoBullet( burst ,dmg, num, spread )

	local info = self.Primary.Info -- get info
	local data = { dmg = dmg, num = num, spread = spread } -- set data

	burst = burst or self.Primary.Burst -- check if burst

	if burst > 1 then -- if burst
		self:SetBurstAmount( burst - 1 )
		self.BurstTbl = data

	SeedDeRandom( "bullet" ) -- Call this to sync random values ( IMPORTANT FOR BULLET SHOTS!!! )

	self.Weapon:EmitSound( self.Primary.Sound ) -- emit sound


	self.Owner:SetAnimation( PLAYER_ATTACK1 )

	self:TakePrimaryAmmo( 1 )

	local bullet = {}
	spread = spread or ( self:OnSights() and self.SETTINGS.sights.cone ) or self.Primary.Cone or 0 -- custom spread function
	for i = 1, num or self.Primary.Num do -- for loop means individual tracking for each bullet

		Dir = (self.Owner:EyeAngles() + self.Owner:GetViewPunchAngles() + Angle(math.Rand(-spread, spread), math.Rand(-spread, spread), 0) * 25):Forward()

		bullet.Num = 1
		bullet.Src = self.Owner:GetShootPos()
		bullet.Dir = Dir
		bullet.Spread = Vector( 0,0,0 )--Vector( math.random( 0, spread ), math.random( 0, spread ), 0 )
		bullet.Tracer = 1
		bullet.TracerName = info.tracer or "taco_tracer_effect"
		bullet.Damage = dmg or math.random( info.damage.min, info.damage.max )
		bullet.Force =  bullet.Damage * 0.9
		bullet.AmmoType = self.Primary.Ammo or "pistol"
		bullet.Callback = function( att, tr, dmg ) -- add a callback for bullet collision
			self:SetDmg( dmg, tr )
			self:BulletCallback( att, tr, dmg, Dir )

		self.Owner:FireBullets( bullet )
	self:PunchScreen( info.punch )

Don’t randomise the Dir param – let the spread key handle the random spread internally. Also, you’re creating a lot of globals, like burst, spread, and Dir. Lastly, I’m not sure how your SeedDeRandom function works, but you’re sending in a constant seed which means you’ll get the same random numbers every time.

Ah yes, thank you for pointing out the globals, I’ve been jumping between python and lua, and it seems that passed my mind, In response to using the spread already provided by ShootBullet, that makes it very difficult to do prediction of bullet paths which is fundamental for how my penetration script and ricochet script perform. and lastly My SeedDeRandom is a function I designed to quickly sync the server and client random values, as you did point out it uses “bullet” as the seed, but it actually will generate a unique value based on CurTime() and the seed you provide. But I fail to understand why those things would mess with client prediction.

CurTime is not guaranteed to be exactly synced between the server and client. Use Player:GetCurrentCommand():CommandNumber().

I found the problem, It was just garry’s mod, After some exstensive testing, I’ve found that all weapons on my localhost server are unable to hit a target that is moving, I’ve seen this before but not on a server. And I will take your suggestion into consideration, in the end all I’ve changed is Dir to local Dir because that was the only variable being declared in a global scope. thanks for the replies.