Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I am trying to make a gun that damages NPC like zombies but not other players, Help?

I have tried this script but it damages other players fight like the NPC as well..... My script (see if you can add anything to make it not damage other player and I can't use teams as I am doing like a "zombie rush" game so no teams): local serverStorage = game:GetService(“ServerStorage”) local replicatedStorage = game:GetService(“ReplicatedStorage”) local KOValue = “Kills” local WOValue = “Wipeouts” local damage = 30 replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part) if game.Workspace[player.Name].Humanoid.Health <= 0 then — The player is dead, do not do anything else local distance = (tool.Handle.CFrame.p – position).magnitude if game.Workspace:FindFirstChild(player.Name..“‘s Trajectory”) then game.Workspace:FindFirstChild(player.Name..“‘s Trajectory”):Destroy() end local trajectory = Instance.new(“Part”,game.Workspace) local smoke = serverStorage.SmokeParticle:Clone() smoke.Parent = tool.Handle trajectory.BrickColor = BrickColor.new(“Institutional white”) trajectory.Material = “SmoothPlastic” trajectory.Name = player.Name..“‘s Trajectory” trajectory.Transparency = 0.5 trajectory.Anchored = true trajectory.Locked = true trajectory.CanCollide = false trajectory.Size = Vector3.new(0.3,0.3,distance) for i = 0,distance,6 do trajectory.CFrame = CFrame.new(tool.Handle.CFrame.p,position) * CFrame.new(0,0,-distance / 2) wait(0.0001) end smoke:Destroy() if part then if part.Name == “Head” or part:IsA(“Hat”) and part.Parent:FindFirstChild(“Humanoid”).Health > 0 then — Boom headshot replicatedStorage.Headshot:FireClient(player) damage = 50 end local humanoid = part.Parent:FindFirstChild(“Humanoid”) if not humanoid then humanoid = part.Parent.Parent:FindFirstChild(“Humanoid”) else humanoid:TakeDamage(damage) if humanoid.Health <= 0 then player.leaderstats[KOValue].Value = player.leaderstats[KOValue].Value + 1 game.Players[humanoid.Parent.Name].leaderstats[WOValue].Value = game.Players[humanoid.Parent.Name].leaderstats[WOValue].Value + 1 end end wait(0.25) if trajectory then trajectory:Destroy() end end end end)

replicatedStorage.EquipAnimation.OnServerEvent:Connect(function(player,animation) local newAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation) newAnim:Play() replicatedStorage.UnequipAnimation.OnServerEvent:Connect(function(player,animation) newAnim:Stop() for i,v in pairs(game.Workspace:GetChildren()) do if v.Name == player.Name..“‘s Trajectory” then v:Destroy() end end end) replicatedStorage.Reload.OnServerEvent:Connect(function(player,animation) newAnim:Stop() local reloadAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation) reloadAnim:Play() end) end)

function checkBodyType(player,tool) if game.Workspace[player.Name]:FindFirstChild(“LowerTorso”) then — R15 tool.shoot.AnimationId = “rbxassetid://936531673” tool.reload.AnimationId = “rbxassetid://937806099” return “R15” end if game.Workspace[player.Name]:FindFirstChild(“Torso”) then — R6 tool.shoot.AnimationId = “rbxassetid://1000874313” tool.reload.AnimationId = “rbxassetid://937933712” return “R6” end end replicatedStorage.CheckBodyType.OnServerInvoke = checkBodyType

4 comments

1

please use a code block

1

yeah, use a code block PLZ

0

yes

0

ok

1 answer

✓ Accepted answer

Ok so I assume you have a check if the hit part has a Humanoid, and we can use that Humanoid's parent(character model) to check if it's the character of any player

local Humanoid = --humanoid
local character = Humanoid.Parent

local isPlayer = false
for i,v in pairs(game.Players:GetPlayers()) do
	if v.Character then
		if v.Character = character then
			isPlayer = true
			--this is the character of a player
		end
	end
end

if isPlayer = false then
	Humanoid:TakeDamage(--damage amount)
end

This code loops through every player and checks if their character is the model you hit, if you it doesn't damage them, otherwise the Humanoid is a NPC, which means you can damage them

2 comments

1

U could use GetPlayerFromCharacter() to check

0

That's a better option, thanks.

This discussion is preserved as it was. New answers, comments, and votes are closed.