FPS gun not working as expected

I couldn’t really find anything on this specific topic, I was making a fps game and I made the gun but I went to make a the gun script and I’m trying to make it so you can only kill zombies and not players so I added this tag (BoolValue) inside called ZombieTag and I did some raycast but it doesn’t seem to work, I tried to debug it the script is inside the gun I can’t figure out what part is wrong inside the script, any help is appreciated! here’s the script let me know if you see any problems in it I am not really that great at raycast so yeah! thank you this is by the way a model not a tool

local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude

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

   local gun = script.Parent 
   local startPosition = gun.Position
   local direction = (mouse.Hit.p - startPosition).unit

   raycastParams.FilterDescendantsInstances = {gun}

   local raycastResult = workspace:Raycast(startPosition, direction * 100, raycastParams)
   if raycastResult and raycastResult.Instance then
   	local hitPart = raycastResult.Instance
   	local humanoid = hitPart:FindFirstChild("Humanoid")
   	local zombieTag = hitPart:FindFirstChild("ZombieTag")

   	if humanoid and zombieTag and humanoid.Health > 0 then
   		humanoid.Health = humanoid.Health - 10 
   	end
   end
end

game:GetService("UserInputService").InputBegan:Connect(function(input, isProcessed)
   if not isProcessed and input.UserInputType == Enum.UserInputType.MouseButton1 then
   	onFire()
   end
end)```
2 Likes

How far does it make it?

1 Like

The raycast? I believe its not making it anywhere, it’s not damaging the zombie so I believe its not making it properly

2 Likes

I mean if you add print statements to your code, how far does it get

1 Like

I tried printing some things like print(“Running”) print(player) etc and it doesn’t print anything

2 Likes

Made some edits on the onFire() function and commented them

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

	local gun = script.Parent 
	local startPosition = gun.Position
	local direction = (mouse.Hit.p - startPosition).unit

	raycastParams.FilterDescendantsInstances = {gun, player.Character} --added the player to be excluded

	local raycastResult = workspace:Raycast(startPosition, direction * 100, raycastParams)
	if raycastResult then --removed the .Instance check because it's a guaranteed property
		local hitParent = raycastResult.Instance.Parent --checking the parent, because if the right arm is hit, the "Humanoid" won't be a child of it
		local humanoid = hitParent:FindFirstChild("Humanoid")
		local zombieTag = hitParent:FindFirstChild("ZombieTag")

		if humanoid and zombieTag and humanoid.Health > 0 then
			humanoid.Health = humanoid.Health - 10 
		end
	end
end

I’d also recommend doing the damage on the server, so create a remote event and do the zombie humanoid damage there instead of on the client because the health wont replicate

2 Likes

It’s a local script right?

2 Likes

Yes I am using a local script that is inside the gun model

1 Like

And you’re not getting any errors? (also did you try swapping out the onFire() function for the one I wrote?)

2 Likes

There is no errors and the zombie is not being damaged, but there is another script that puts the gun inside the camera I think that might interfere or is it alright? I tried the OnFire() function that you wrote and it still seems around the same result

local localPlayer = game.Players.LocalPlayer
local cam = workspace.CurrentCamera

game.Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson


local arms = game.ReplicatedStorage.ViewmodelArms:Clone()
arms.Parent = cam

local lshoulder = arms.LeftUpperArm.LeftShoulder
local rshoulder = arms.RightUpperArm.RightShoulder


local gun = game.ReplicatedStorage.Gun:Clone()
gun.Parent = arms

local m6d = Instance.new("Motor6D")
m6d.C0 = CFrame.new(1, -0.3, -2.3)
m6d.Part0 = arms.Head
m6d.Part1 = gun.PrimaryPart
m6d.Parent = arms.Head


local ads = false

game.Players.LocalPlayer:GetMouse().Button2Down:Connect(function()	
	ads = true
end)

game.Players.LocalPlayer:GetMouse().Button2Up:Connect(function()	
	ads = false
end)


game:GetService("RunService").RenderStepped:Connect(function()
	
	arms.Head.CFrame = cam.CFrame
	
	local aimC1 = m6d.C0 - Vector3.new(0.177, 0.34, -1)
	
	local c1 = ads and aimC1 or CFrame.new()
	m6d.C1 = m6d.C1:Lerp(c1, 0.2)
	
	
	local cf = gun.Left.CFrame * CFrame.Angles(math.rad(90), 0, math.rad(20)) * CFrame.new(0, 1.3, 0)
	lshoulder.C1 = cf:Inverse() * lshoulder.Part0.CFrame * lshoulder.C0
	
	local cf = gun.Right.CFrame * CFrame.Angles(math.rad(90), 0, math.rad(-10)) * CFrame.new(0, 1.3, 0)
	rshoulder.C1 = cf:Inverse() * rshoulder.Part0.CFrame * rshoulder.C0
end)
1 Like

On your first script at the end, if you put a print statement inside the InputBegan connection does anything print?

example:

game:GetService("UserInputService").InputBegan:Connect(function(input, isProcessed)
   print("hello world")
   if not isProcessed and input.UserInputType == Enum.UserInputType.MouseButton1 then
   	onFire()
   end
end)
2 Likes

Nothing seems to be printing inside output and again could that script be interfering with how it is working?

1 Like

They are two separate scripts though right? What’s your workspace setup like?

2 Likes

Yes those are two separate ones, there is a local script called “ViewmodelHandler” inside StarterCharacterScripts and then in replicatedStorage there is a model called “Gun” and then inside of it a local script called GunScript, there’s also viewmodelarms inside replicatedStorage and just a zombie inside workspace

1 Like

Ok hm that’s a lot of moving parts, hard to keep track xD. How difficult would it be to combine all of those scripts into one?

1 Like

I could try to combine the viewmodelHandler script with the gun script or something like that

I think the result is the same :sweat_smile: I am not sure why the raycast is like that, the arms still works if you combine them and the gun still is in the player’s hands but it doesn’t shoot properly