My gun isn't working to kill zombies

I am making a gun to kill zombies, but it isn’t killing the zombies.

Here’s a clip of my gun: robloxapp-20220227-2029022.wmv (2.9 MB)

I’ve watched this tutorial to make the gun:

Client script:

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

mouse.Button1Down:Connect(function()
	script.Parent.GunEvent:FireServer(mouse.Hit.p)
end)

Server script:

local tool = script.Parent
local handle = tool.Handle

local enabled = true

local shotSFX = handle.Shot
local shotEffect = handle.Attachment.ParticleEmitter
local shotLight = handle.Attachment.PointLight

tool.Activated:Connect(function()
	if not enabled then return end
	enabled = false
	shotSFX:Play()
	shotEffect.Enabled = true
	shotLight.Enabled = true
	wait(0.1)
	shotLight.Enabled = false
	shotEffect.Enabled = false
	wait(0.2)
	enabled = true
end)

tool.GunEvent.OnServerEvent:Connect(function(player, mousePos)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	local raycastResult = workspace:Raycast(script.Parent.Handle.Position, (mousePos - script.Parent.Handle.Position*300),raycastParams)
	
	if raycastResult then
		local hitPart = raycastResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
		
		if model then
			if model:FindFirstChild("Humanoid") then
				model.Humanoid.Health -= 30
			end
		end
	end
end)
3 Likes

Print hitPart, so you can narrow down what the issue is

It says Part or Union when I shoot the zombie

Then it might be hitting a random part of your map, try changing a few lines of code

script.Parent.GunEvent:FireServer(mouse.Hit.p)

to

script.Parent.GunEvent:FireServer(mouse.Hit.Position)

and try this

tool.GunEvent.OnServerEvent:Connect(function(player, mousePos)
	
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {player.Character}
	raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

        local origin = script.Parent.Handle.Position
        local direction = (mousePos - origin).Unit
	
	local raycastResult = workspace:Raycast(origin,direction * 300,raycastParams)
	
	if raycastResult then
		local hitPart = raycastResult.Instance
		local model = hitPart:FindFirstAncestorOfClass("Model")
                local hum = model:FindFirstChildOfClass("Humanoid")
		
		if model and model.Name ~= player.Name and hum then
			hum:TakeDamage(30)
		end
	end
end)
1 Like

So now how do I make the gun not kill the players?

name the zombie’s humanoid into something else or do something with this Players:GetPlayerFromCharacter

i’ve tried to name the humanoid to zombie and tried to use Players:GetPlayerFromCharacter but isn’t working.

show me your script for both.


try

if not game.Players:GetPlayers()[humanoid.Parent.Name] then
      --code here
end

(mousePos - script.Parent.Handle.Position*300)
I think it should be
(mousePos - script.Parent.Handle.Position).Unit * 300

i mentioned this problem in my 2nd reply

You’ll have to define Players somewhere above if you havent already, local Players = game:GetService(“Players”)

if model then
	if model:FindFirstChild("Humanoid") and not Players:GetPlayerFromCharacter(model) then
		model.Humanoid:TakeDamage(30)
	end
end
1 Like

I didn’t bother reading any other replies before responding sorry

1 Like

Roblox doesn’t have -= += *= -- or ++ you need to either do Humanoid:TakeDamage(damage) or manually do it like this

Humanoid.Health = Humanoid.Health - 30

Wrong. Roblox does have -= +- *=

However, -- is for commenting. And ++ doesn’t exist.

I’ve made some changes at this part, and is working now, thanks for your help :slight_smile:

No they don’t, Roblox uses an older version of LUA that doesn’t have those operators.

Proof

image

In the example you have it doesn’t work, but rewritten the += operator works fine

Screenshot

Screenshot (3535)

Just tested it out, only has a syntax error when you use it inside print.
When did they add this?

Read this article:

1 Like