Problems with gunplay and spherecast not detecting moving character

Hello developers, I am currently working on a FPS game and I’m currently struggling to make good gunplay. whenever 2 players are moving at the same time, the spherecast that I use to create the bullet does not detect anything and shoots in front of the player…?

here is a video

here is my shooting server script inside of server script service

local tweenservice = game:GetService("TweenService")

local shootEvent = game.ReplicatedStorage.Events.Shoot
game:GetService("PhysicsService"):CollisionGroupSetCollidable("ShootingRaycast", "Accessories", false)
game:GetService("PhysicsService"):CollisionGroupSetCollidable("ShootingRaycast", "VM", false)

local bulletholePreset = game.ReplicatedStorage.Assets.BulletHole

local rayParams = RaycastParams.new()

shootEvent.OnServerEvent:Connect(function(player, character,  damage, mousePos, vmname, headshotMultiplier, cooldown, armpos, bulletsize, finalpos)
	if character then
		rayParams.FilterType = Enum.RaycastFilterType.Exclude
		rayParams.FilterDescendantsInstances = {character}
		rayParams.CollisionGroup = "ShootingRaycast"		
		
		local dir = (mousePos - character.Head.Position).Unit * 1023
		
		local ray = workspace:Spherecast(character.Head.Position, bulletsize, dir, rayParams)
		if ray then
			if ray.Instance then
				if ray.Instance.Parent.Name ~= character.Name or ray.Instance.Parent.Parent.Name ~= character.Name then
					if ray.Instance.Parent:FindFirstChild("Humanoid") or ray.Instance.Parent.Parent:FindFirstChild("Humanoid") then
						if ray.Instance.Name == "Handle" then
							if ray.Instance.Parent.Parent:FindFirstChild("Humanoid").Health ~= 0  then
								ray.Instance.Parent.Parent:FindFirstChild("Humanoid"):TakeDamage(damage * headshotMultiplier)
								if ray.Instance.Parent.Parent.Humanoid.Health <= 0 then
									shootEvent:FireClient(player, "dead")
								else
									shootEvent:FireClient(player, "head")
									if game.Players:GetPlayerFromCharacter(ray.Instance.Parent) then
										game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("DamageEvent"):FireClient(game.Players:GetPlayerFromCharacter(ray.Instance.Parent), ray.Instance.Position)
									end
								end
							end
						elseif ray.Instance.Name == "Head" then
							if ray.Instance.Parent.Humanoid.Health ~= 0 then
								ray.Instance.Parent.Humanoid:TakeDamage(damage * headshotMultiplier)
								if ray.Instance.Parent.Humanoid.Health <= 0 then
									shootEvent:FireClient(player, "dead")
								else
									shootEvent:FireClient(player, "head")
									if game.Players:GetPlayerFromCharacter(ray.Instance.Parent) then
										game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("DamageEvent"):FireClient(game.Players:GetPlayerFromCharacter(ray.Instance.Parent), ray.Instance.Position)
									end
								end
							end
						else
							if ray.Instance.Parent.Humanoid.Health ~= 0 then
								ray.Instance.Parent.Humanoid:TakeDamage(damage)
								if ray.Instance.Parent.Humanoid.Health <= 0 then
									shootEvent:FireClient(player, "dead")
								else
									shootEvent:FireClient(player, "body")
									if game.Players:GetPlayerFromCharacter(ray.Instance.Parent) then
										game:GetService("ReplicatedStorage"):WaitForChild("Events"):WaitForChild("DamageEvent"):FireClient(game.Players:GetPlayerFromCharacter(ray.Instance.Parent), ray.Instance.Position)
									end
								end
								
							end
						end
					else
						local newbullethole = bulletholePreset:Clone()
						newbullethole.CFrame = CFrame.new(ray.Position, ray.Position + ray.Normal)
						newbullethole.Parent = workspace.BulletHoles
					end
				end
			end
		end
		if not character.HumanoidRootPart:FindFirstChild(vmname.."Shoot") then
			local newsound = game.ReplicatedStorage.Viewmodels:FindFirstChild(vmname).SFX.Shoot:Clone()
			newsound.Parent = character.HumanoidRootPart
			newsound:Play()
			newsound.Name = vmname.."Shoot"
		else
			character.HumanoidRootPart:FindFirstChild(vmname.."Shoot"):Play()
		end
	end
end)

if anyone knows a solution to this problem, please help me out. thank you!

i would like to note that this happens with raycasting too, not just spherecasting.

You should be performing raycast/spherecast on the client, and sending relevant data to the server to validate if you want fast and responsive gunplay. You could send over the direction/origin vector, and whatever part you hit. If you want, you can use another raycast on the server to see if the shot was possible (to make sure they’re not shooting through walls), but to have a responsive game, to some degree, you need to give the client some authority to make decisions. Servers job should be to validate the legitimacy of the information its been given.

hmm okay ill look into this. thank you for your help!

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