Why my gun not working?

Alright so I made a gun and it works it can shoot and everything but the issue is it isn’t dealing damage… Why could that be?

– Player script

-- Services

local runservice = game:GetService("RunService")
local uis = game:GetService("UserInputService")
local replicatedstorage = game:GetService("ReplicatedStorage")

-- Variables

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local camera = workspace.CurrentCamera
local mouse = player:GetMouse()

local gunsystemFolder = replicatedstorage:WaitForChild("GunSystem")
local viewmodelFolder = gunsystemFolder:WaitForChild("Viewmodels")
local eventsFolder = gunsystemFolder:WaitForChild("Events")

-- Viewmodels/States

local currentVM = nil

local idle = true
local walking = false
local shooting = false
local fullautoshooting = false
local inair = false
local canshoot = true
local canreload = true
local reloading = false

character.Humanoid:GetPropertyChangedSignal("MoveDirection"):Connect(function()
	if currentVM then
		if character.Humanoid.MoveDirection.Magnitude > 0 then
			if not inair then
				walking = true
				idle = false
			end
		else
			walking = false
			idle = true
		end
	end
end)

-- Animations

local loadedanims = {
	shoot = nil,
	reload = nil,
	inspect = nil
}

-- Inventory

local inventory = {
	primary = "",
	secondary = "Glock",
	melee = "",
	module = nil
}

-- Sway

local swaycf = CFrame.new()
local lastcameracf = CFrame.new()

-- Functions

local function cancelReload()

end

local function loadSlot(weapon)
	if weapon and weapon ~= inventory.module then
		if camera:FindFirstChildWhichIsA("Model") then
			currentVM:Destroy()
			currentVM = nil
		end
		if not viewmodelFolder:FindFirstChild(weapon) then
			warn("Can't find viewmodel")
			return
		end
		local newVM = viewmodelFolder:FindFirstChild(weapon):Clone()
		newVM.Parent = camera
		currentVM = newVM
		inventory.module = require(currentVM.Module)

		loadedanims.shoot = newVM.Humanoid:LoadAnimation(newVM.Animations.Shoot)
		loadedanims.reload = newVM.Humanoid:LoadAnimation(newVM.Animations.Reload)
		loadedanims.inspect = newVM.Humanoid:LoadAnimation(newVM.Animations.Inspect)
	end
end

local function reload()

end

local function spread(axis, angle)
	local cosAngle = math.cos(angle)
	local z = 1-math.random() * (1-cosAngle)
	local phi = math.random() * math.pi * 2
	local r = math.sqrt(1-z*z)
	local x = r * math.cos(phi)
	local y = r * math.sin(phi)
	local vec = Vector3.new(x,y,z)
	
	if axis.Z > 0.9999 then
		return vec
	elseif axis.Z < 0.999 then
		return -vec
	end
	local orth = Vector3.zAxis:Cross(axis)
	local rot = math.acos(axis:Dot(Vector3.zAxis))
	return CFrame.fromAxisAngle(orth, rot) * vec
	
end

local function shoot()
	if currentVM then
		if canshoot then
			canshoot = false
			loadedanims.shoot:Play()
			currentVM.Ammo.Value -= 1
			loadedanims.inspect:Stop()
			
			local rayParams = RaycastParams.new()
			rayParams.FilterType = Enum.RaycastFilterType.Exclude
			rayParams.FilterDescendantsInstances = {character}
			rayParams.CollisionGroup = "ShootingCast"
			
			local finalspread = spread(currentVM.CameraPart.CFrame.LookVector, math.rad(inventory.module.spread))
			
			local dir = finalspread * 999999
			
			local ray = workspace:Raycast(character.Head.Position, dir, rayParams)
			
			if ray and ray.Instance then
				game.ReplicatedStorage.GunSystem.Events.ShootEvent:FireServer(ray.Instance, inventory.module.damage)
			end
			canshoot = true
		end
	end
end

-- RenderStepped

runservice.RenderStepped:Connect(function()
	if character then
		if currentVM then
			local rot = camera.CFrame:ToObjectSpace(lastcameracf)
			local x, y, z = rot:ToOrientation()

			swaycf = swaycf:Lerp(CFrame.Angles(math.sin(x) * 3, math.sin(y) * 3, 0), 0.05)
			lastcameracf = camera.CFrame

			local bobx
			local boby

			if walking and not inair then
				bobx = math.sin(time() * 7.5) / 20
				boby = math.cos(time() * (7.5 * 2)) / 20
			elseif idle and not inair then
				bobx = math.sin(time() * 0.75) / 120
				boby = math.cos(time() * (0.75 * 2)) / 120
			else
				bobx = 0
				boby = 0
			end

			local finalbob = CFrame.new(bobx, boby, 0)
			currentVM:SetPrimaryPartCFrame(camera.CFrame * swaycf * finalbob)
		end
		if character.Humanoid.FloorMaterial == Enum.Material.Air then
			inair = true
		else
			inair = false
		end
	end
end)

-- User Inputs

uis.InputBegan:Connect(function(input, gamepro)
	if gamepro then return end
	if input.KeyCode == Enum.KeyCode.One then
		loadSlot(inventory.primary)
	end
end)

uis.InputBegan:Connect(function(input, gamepro)
	if gamepro then return end
	if input.KeyCode == Enum.KeyCode.Two then
		loadSlot(inventory.secondary)
	end
end)

uis.InputBegan:Connect(function(input, gamepro)
	if gamepro then return end
	if input.KeyCode == Enum.KeyCode.Three or input.KeyCode == Enum.KeyCode.Q then
		loadSlot(inventory.melee)
	end
end)

mouse.Button1Down:Connect(function()
	if currentVM then
		if currentVM.Ammo.Value <= currentVM.MaxAmmo.Value and currentVM.Ammo.Value ~= 0 then
			if not inventory.module.fullauto then
				shoot()
			else
				fullautoshooting = true
			end
		end
	end
end)

mouse.Button1Up:Connect(function()
	if currentVM then
		if inventory.module.fullauto then
			fullautoshooting = false
		end
	end
end)

– Server Script

game.ReplicatedStorage.GunSystem.Events.ShootEvent.OnServerEvent:Connect(function(player, hitpart, damage)
	if hitpart.Parent:FindFirstChild("Humanoid") then
		if hitpart.Parent:FindFirstChild("Humanoid").Health > 0 then
			hitpart.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
		end
	end
end)

– Gun Module Script

local Settings = {
	
	damage = 24;
	cooldown = 0.1;
	
	fullauto = false;
	
	spread = 0.2;
}

return Settings

I haven’t completely finished the local script but it should shoot but it isn’t damaging anything

1 Like

I know this isn’t really helpful, but there’s several possible reasons.

  • It could be failing to hit anything because there’s nothing in the CollisionGroup or it’s not set up right.
  • The ray might not be returning anything or missing, thus not firing the remote event to the server.
  • The part you hit with the ray might not be a direct descendant of the character model, thus not detecting the humanoid.

Do you know how to use the debugger and watch tool? You could probably solve this rather quickly with two breakpoints.

1 Like

Can the characters you’re trying to hit collide with the collision group ShootingCast?
Try printing ray and ray.Instance

Also your code is not really secure as exploiters can just fire your remote event to deal whatever damage they want to whatever they want.

1 Like

So what are you suggesting I do?

1 Like

here is my shooting cast settings

image

1 Like