How can I damage an ai zombie with a gun tool when its networkownership is nil?

  1. What do you want to achieve? Keep it simple and clear!
    I want my gun to be able to damage a zombie ai character.
  2. What is the issue? Include screenshots/videos if possible!
    I set the network ownership of the zombie to nil to make it so my zombie ai can’t do this glitching walking problem. The problem is that since it is nil my gun for some reason a player with a gun tool can’t damage my zombie ai, I tested it to make sure it was the network ownership that was causing the problem and it did.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I tried to set the network ownership of the player to nil which fixed the problem. But made the player controls really wonky and very hard for a player to even play the game. I want to make it so I can eliminate the glitchy walking from the ai without having to change the network ownership of the player makes the player controls really weird. Is there a different way to do this/solution? Sorry for my bad grammar, I am quite young.

zombie ai script

local Character = script.Parent

local Humanoid = Character:FindFirstChild("Humanoid")

local CanDamage = true

Character.PrimaryPart:SetNetworkOwner(nil)


function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 100000000
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent) then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			local player = game.Players:GetPlayerFromCharacter(human.Parent)
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and player then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end

Humanoid.Touched:Connect(function(hit)
	local Humanoid = hit.Parent:FindFirstChild("Humanoid") or hit.Parent.Parent:FindFirstChild("Humanoid")
	
	if Humanoid then
		local Player = game.Players:GetPlayerFromCharacter(Humanoid.Parent)
		
		if Player then
			if CanDamage == true then
				CanDamage = false
				Humanoid.Health -= 15
				wait(0.1)
				CanDamage = true
			end
		end
	end
end)


while true do
	wait(0.1)
	local NearestTorso = findNearestTorso(Character.HumanoidRootPart.Position)
	
	if NearestTorso == nil then
		print("do nothing")
	else

		local Player = game.Players:GetPlayerFromCharacter(NearestTorso.Parent) or nil

		if Player or NearestTorso.Parent.Name ~= NearestTorso.Parent.Name.." Robot" then
			Humanoid:MoveTo(NearestTorso.Position)
		elseif not Player then
			print("do nothing")
		end
	end
	
end

local script: Gun script(note: some variables in the local script are not needed so ignore those, that is because I have a folder that holds all of the gun data)

local Holdanimation = script.Parent:WaitForChild("HoldAnimation")

local player = game.Players.LocalPlayer

local mouse = player:GetMouse()

local Tool = script.Parent

local CanShoot = true

local Configurations = Tool:WaitForChild("Configurations")

local Ammo = Configurations:WaitForChild("Ammo").Value

local Damage = Configurations:WaitForChild("Damage").Value

local ShootEvent = Tool:WaitForChild("Shoot")

local Reloadtime = Configurations:WaitForChild("ReloadTime").Value

local MaxAmmo = Configurations:WaitForChild("MaxAmmo").Value

local Firerate = Configurations:WaitForChild("FireRate").Value

local UserInputService = game:GetService("UserInputService")

local reloading = false

script.Parent.Equipped:Connect(function()
	local Humanoid = script.Parent.Parent:WaitForChild("Humanoid")
	HoldAnimationTrack = Humanoid:LoadAnimation(Holdanimation)
	HoldAnimationTrack:Play()
end)

Tool.Activated:Connect(function()
	if not reloading and Ammo > 0 then
		if CanShoot then
			local mousepos = mouse.Hit.Position
			local origin = Tool.Handle.Tip.Position
			Ammo = Ammo - 1
			print(Ammo)
			CanShoot = false
			ShootEvent:FireServer(mousepos,origin)
			wait(Firerate)
			CanShoot = true
		end
	elseif Ammo <= 0 and reloading == false then
		print("reloading")
		reloading = true
		Tool.Handle.Reload.Playing = true
		wait(Reloadtime)
		print("finished reloading")
		Ammo = MaxAmmo
		reloading = false
	end
end)

script.Parent.Unequipped:Connect(function()
	HoldAnimationTrack:Stop()
end)

Gun script server

local Tool = script.Parent

local Configurations = Tool:WaitForChild("Configurations")

local Ammo = Configurations:WaitForChild("Ammo")

local Damage = Configurations:WaitForChild("Damage").Value

local ShootEvent = Tool:WaitForChild("Shoot")

local Debris = game:GetService("Debris")

local range = Configurations:WaitForChild("Range")

local TweenService = game:GetService("TweenService")

local function CreateBullet(origin, direction)
	local midpoint = origin + direction/2
	
	local part = Instance.new("Part")
	part.Parent = game.Workspace
	part.Anchored = true
	part.CanCollide = false
	part.BrickColor = BrickColor.new("Yellow flip/flop")
	part.Transparency = 0
	part.Material = Enum.Material.Plastic
	
	
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(.2,.2,direction.magnitude)
	
	Debris:AddItem(part,0.2)
end

local function CreateBulletEffect(origin, direction, pos,tip,goal)
	local midpoint = origin + direction/2
	
	local tweeninfo = TweenInfo.new(.2, Enum.EasingStyle.Linear)

	local part = Instance.new("Part")
	part.Parent = game.Workspace
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = 0
	part.Material = Enum.Material.Neon
	
	part.Position = Vector3.new(part.Parent) + Vector3.new(tip)
	part.CFrame = CFrame.lookAt(tip,goal)
	part.Size = Vector3.new(.3,.3,5)
	local tween = TweenService:Create(part, tweeninfo,pos)
	tween:Play()
	tween.Completed:Connect(function()
		part:Destroy()
	end)
	
	
	
	
end


ShootEvent.OnServerEvent:Connect(function(player,MousePosition,TipPos)
	local direction = (MousePosition - TipPos).Unit * range.Value
	
	local result = workspace:Raycast(TipPos, direction)
	Tool.Handle.Shoot:Play()
	if result then
		local Humanoid = result.Instance.Parent:FindFirstChild("Humanoid") or result.Instance.Parent.Parent:FindFirstChild("Humanoid")
		if Humanoid then
			local ModelName = Humanoid.Parent.Name
			if game.Players:FindFirstChild(ModelName) then
				-- Do nothing
			else
				Humanoid.Health = Humanoid.Health - Damage
			end
		end
	end
	
	CreateBullet(TipPos, direction)
	
end)

let me know if you have any questions or want me to change a the category

Try switching out your server script with this and tell me if it prints anything when you shoot a zombie:

--//Services
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

--//Variables
local Tool = script.Parent
local ShootEvent = Tool:WaitForChild("Shoot")
local Configurations = Tool:WaitForChild("Configurations")

local Ammo = Configurations:WaitForChild("Ammo")
local Damage = Configurations:WaitForChild("Damage").Value
local range = Configurations:WaitForChild("Range")

--//Controls
local tweeninfo = TweenInfo.new(0.2, Enum.EasingStyle.Linear)

--//Functions
local function CreateBullet(origin, direction)
	local midpoint = origin + direction/2

	local part = Instance.new("Part")
	part.Anchored = true
	part.CanCollide = false
	part.BrickColor = BrickColor.Yellow()
	part.Transparency = 0
	part.Material = Enum.Material.Plastic
	part.CFrame = CFrame.new(midpoint, origin)
	part.Size = Vector3.new(0.2, 0.2, direction.Magnitude)
	part.Parent = workspace
	
	task.delay(0.2, part.Destroy, part)
end

local function CreateBulletEffect(origin, direction, pos, tip, goal)
	local midpoint = origin + direction/2
	
	local part = Instance.new("Part")
	part.Anchored = true
	part.CanCollide = false
	part.Transparency = 0
	part.Material = Enum.Material.Neon
	part.Position = Vector3.new(part.Parent) + Vector3.new(tip)
	part.CFrame = CFrame.lookAt(tip, goal)
	part.Size = Vector3.new(0.3, 0.3, 5)
	part.Parent = workspace
	
	local tween = TweenService:Create(part, tweeninfo, pos)
	tween:Play()
	tween.Completed:Wait()
	
	part:Destroy()
end

ShootEvent.OnServerEvent:Connect(function(player, MousePosition, TipPos)
	if not MousePosition or not TipPos then
		return
	end
	
	Tool.Handle.Shoot:Play()

	local direction = (MousePosition - TipPos).Unit * range.Value

	local result = workspace:Raycast(TipPos, direction)
	
	if result then
		local Humanoid = result.Instance.Parent:FindFirstChildWhichIsA("Humanoid") or result.Instance.Parent.Parent:FindFirstChildWhichIsA("Humanoid")
		
		if Humanoid then
			local player = Players:GetPlayerFromCharacter(Humanoid.Parent)
			
			if not player then
				Humanoid.Health -= 10
				print("Damaged")
			end
		end
	end

	CreateBullet(TipPos, direction)
end)

when the zombie is far away from the player. It will do damage and print “Damaged”. But when it gets close, the player can’t do any damage to the zombie at all.

When you’re close, does the bullet still visually hit the zombie or no?

yes, it still hits the zombie.(I actually have to go soon)

Could you right click the gun model and save it as a file, and then send it to me so I can help quicker?

ok, I actually have to go right now, could we continue this tommorow?
GunModel.rbxm (11.5 KB)

Sure, I’ll try to fix it while you’re gone.

1 Like

Fixed version:
GunModel.rbxm (10.9 KB)

I made it more secure and everythings fixed.

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