Gun doesn't damage players when players are moving some times

just as like the title said, heres the local script

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local ReloadAnim
local ReloadTrack = nil
local tool = script.Parent
local handle = tool:FindFirstChild(“Handle”)
local larrel = handle and handle:FindFirstChild(“larrel”)
local debounce = false

local ammo = 34
local maxAmmo = 34
local reloading = false

– Reload
local function reload ()
if reloading == false then
reloading = true

	if ReloadTrack then
		ReloadTrack:Play()
	end
	local part = Instance.new("Part")
	part.Position = tool.Handle.Position
	part.Size = Vector3.new(0.201, 0.776, 0.32)
	part.CFrame = tool.Handle.CFrame * CFrame.Angles(0, math.rad(90), 0)
	part.Parent = workspace
	part.CanCollide = true
	part.BrickColor = BrickColor.new("Smoky grey")
	part.Material = Enum.Material.Metal
	script.Parent.Handle["Gun Reload"]:Play()
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	task.wait(3)
	game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
	ammo = maxAmmo
	player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
	player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
	if ReloadTrack then
		ReloadTrack:Stop()
	end
	reloading = false
	wait(4)
	part:Destroy()
end

end

– Shooting
tool.Activated:Connect(function()
if debounce == false and ammo > 0 and reloading == false then
debounce = true
ammo -= 1

	tool.Shoot:FireServer(mouse.Hit.Position)
	script.Parent.Handle.Barrel["Gun shot"]:Play()
	workspace.CurrentCamera.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(math.rad(2),0,0)
	player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Text = ""..ammo.."/"..maxAmmo
	player.PlayerGui.AmmoGui3.Frame3.TextLabelg.Transparency = 0
	task.wait(0.1)
	debounce = false
elseif ammo <= 0 and reloading == false then
	reload()
end

end)

– Equip Function
tool.Equipped:Connect(function()
end)

– Unequip Function
tool.Unequipped:Connect(function()
end
end)

heres the server script

local tool = script.Parent
local rep = game.ReplicatedStorage
local bulletline = rep.bullet
local bulletlinecloe = bulletline:Clone()
local animation = script.headhot
– Raycast Function
script.Parent.Shoot.OnServerEvent:Connect(function(player, mousePosition)
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character}
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

local raycastResult = workspace:Raycast(tool.Handle.Barrel.Position, (mousePosition - tool.Handle.Barrel.Position).Unit * 42, raycastParams)

while raycastResult and raycastResult.Instance:IsA("Accessory") do
	raycastResult = workspace:Raycast(raycastResult.Position, (mousePosition - raycastResult.Position).Unit * 42, raycastParams)
end

if raycastResult then
	local raycastInstance = raycastResult.Instance
	local model = raycastInstance:FindFirstAncestorOfClass("Model")
	local wood = raycastInstance.Material == Enum.Material.Wood
	local metal = raycastInstance.Material == Enum.Material.Metal
	local smooth = raycastInstance.Material == Enum.Material.SmoothPlastic
	local humanoid = model and model:FindFirstChildWhichIsA("Humanoid")
	
		if humanoid then
			if raycastInstance.Name == "Head" then
				local anmier = model:FindFirstChild("Humanoid"):WaitForChild("Animator")
				local play = anmier:LoadAnimation(animation)
				humanoid:TakeDamage(56)
				play:Play()
				script.Parent.Handle.hit:Play()
				local ind = Instance.new("Part")
				ind.Parent = workspace
				ind.CFrame = CFrame.new(raycastResult.Position)
				ind.Size = Vector3.new(1.158, 0.45, 0.979)
				ind.Material = Enum.Material.Ground
				ind.BrickColor = BrickColor.new("Nougat")
				ind.CanCollide = false
				ind.Anchored = false
				ind.Transparency =  1
				local partiec = Instance.new("ParticleEmitter", ind)
				partiec.Rate = 100
				partiec.Size = NumberSequence.new(1.0)
				partiec:Emit(100)
				partiec.Texture = "rbxassetid://5069294691"
				partiec.Color = ColorSequence.new(Color3.fromRGB(85, 0, 0))
				partiec.EmissionDirection = "Top"
				partiec.Transparency = NumberSequence.new(0.3)
				partiec.Rotation = NumberRange.new(3,3)
				partiec.RotSpeed = NumberRange.new(2,2)
				partiec.Lifetime = NumberRange.new(0.8,0.8)
				partiec.Speed = NumberRange.new(8, 8)
				partiec.RotSpeed = NumberRange.new(2, 2)
				partiec.SpreadAngle = Vector2.new(100,100)
				task.wait(0.3)
				ind:Destroy()
				partiec:Destroy()
			else
				humanoid:TakeDamage(36)
				script.Parent.Handle.hit:Play()
				local ind = Instance.new("Part")
				ind.Parent = workspace
				ind.CFrame = CFrame.new(raycastResult.Position)
				ind.Size = Vector3.new(1.158, 0.45, 0.979)
				ind.Material = Enum.Material.Ground
				ind.BrickColor = BrickColor.new("Nougat")
				ind.CanCollide = false
				ind.Anchored = false
				ind.Transparency =  1
				local partiec = Instance.new("ParticleEmitter", ind)
				partiec.Rate = 100
				partiec.Size = NumberSequence.new(1.0)
				partiec:Emit(100)
				partiec.Texture = "rbxassetid://5069294691"
				partiec.Lifetime = NumberRange.new(0.8,0.8)
				partiec.Color = ColorSequence.new(Color3.fromRGB(85, 0, 0))
			partiec.EmissionDirection = "Top"
			partiec.Transparency = NumberSequence.new(0.3)
			partiec.Rotation = NumberRange.new(3,3)
			partiec.RotSpeed = NumberRange.new(2,2)
			partiec.Speed = NumberRange.new(8, 8)
			partiec.RotSpeed = NumberRange.new(2, 2)
			partiec.SpreadAngle = Vector2.new(100,100)
			task.wait(0.3)
			ind:Destroy()
			partiec:Destroy()
		end
	end
end

end)

script.Parent.Shoot.OnServerEvent:Connect(function(player, mousePosition)
script.Parent.Handle.larrel.SurfaceLight.Enabled = true
script.Parent.Handle.larrel.ParticleEmitter.Enabled = true
script.Parent.Handle.theslidle.Transparency = 1
script.Parent.Handle.theslidle2.Transparency = 0
local bulletlinecloe = bulletline:Clone()
bulletlinecloe.Position = tool.Handle.theslidle.Position
bulletlinecloe.Parent = workspace
bulletlinecloe.CFrame = tool.Handle.CFrame * CFrame.Angles(0, math.rad(180),0)
bulletlinecloe.CFrame = tool.Handle.CFrame * CFrame.new(0,0,-23)
wait(0.2)
bulletlinecloe:Destroy()
script.Parent.Handle.theslidle.Transparency = 0
script.Parent.Handle.theslidle2.Transparency = 1
script.Parent.Handle.larrel.SurfaceLight.Enabled = false
script.Parent.Handle.larrel.ParticleEmitter.Enabled = false
print(“it works”)
local part = Instance.new(“Part”)
part.Position = tool.Handle.theslidle.Position
part.Size = Vector3.new(0.052, 0.044, 0.17)
part.CFrame = tool.Handle.CFrame * CFrame.Angles(0, math.rad(90), 0)
part.Parent = workspace
part.CanCollide = true
part.BrickColor = BrickColor.new(“Bronze”)
part.Material = Enum.Material.Metal
wait(4)
part:Destroy()
end)

tool.Equipped:Connect(function()
end)
end)

Could just be an internet speed issue? You could try making hitboxes bigger

really?.. the code i got was from a video but i changed the code a bit for effects and one of the commnets says that it could be the remote event delay and one time one of my friend’s shooted the gun at me like 6 times an it didn’t do damage some how, but i guess i can make hitboxes bigger!

Well yeah, it takes time for a client to send information to a server and to receive information from a server. That time is what is called ping.

If you turn on performance stats in settings you can see your ping. If its 200ms there is most likely gonna be a huge problem as the Roblox character walk speed is studs per second and casting delayed rays 0.2+ seconds later probably won’t hit a moving target.

oh i guess your right it’s the ping because its so high when i test the game

You won’t have issues playtesting in studio because it creates a “fake” server on your computer.
And sending information from your computer (client) to your computer (server) is ~0ms so its almost instant

so your saying when i play my game is studio it doesn’t have the 0.2 counting and when i play the game in roblox it waits 0.2?

Well yeah, but it isn’t exactly 200ms it varies depending on internet speed.
I’m guessing normal speed is around 50ms - at which you shouldn’t have issues, but 50-150 shouldn’t cause issues either.
And internet speed depends on location and ISP, which you can’t control where your players play their games from or who provides them internet so it’s not your fault.

You could make some workarounds to make the experience smooth for users with all internet speeds but I doubt it would be exploiter-proof and might cost performance

1 Like

oh well thanks for telling me!!