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

Bullets not doing damage when touched by a player/npc?

Hi, I've been trying to make a gun but it doesn't damage players. Local Script

local Gui = script.Parent:WaitForChild("ScreenGui")
local Ammo = 4
local WaitTime = 1
local StoredAmmo = 3
local ShootPart = script.Parent.ShootPart
local mouse = game.Players.localPlayer:GetMouse()
script.Parent.Parent.Equipped:Connect(function(Mouse)
	        animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
	   	   GuiClone = Gui:Clone()
	   	   GuiClone.Parent = game:GetService("Players").LocalPlayer.PlayerGui
	        animation:Play()
		   animation.Looped = true
end)
script.Parent.Parent.Activated:Connect(function(Mouse)
	  animation2 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation2)
	  GuiClone.Frame.TextLabel.Text = Ammo.."|"..StoredAmmo
	  GuiClone.Enabled = true
	  script.Parent.Sound:Play()
	  ammoCheck()
	  Ammo = Ammo - 1
	  shoot()
	  animation2:Play()
	  wait(WaitTime)
end)

script.Parent.Parent.Unequipped:Connect(function()
	 animation:Stop()
	 animation.Looped = false
	GuiClone.Enabled = false
end)
function reload()
	StoredAmmo = StoredAmmo - 1
	Ammo = 4
	animation3 = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation3)
	animation3:Play()
	if StoredAmmo < 1 then
		GuiClone.Frame.TextLabel.Text = "No more ammo"
		wait(1.5)
		animation:Stop()
		animation2:Stop()
		GuiClone:Destroy()
		script.Parent.Parent:Destroy()
	end
end
function ammoCheck()
	if Ammo < 1 then
		reload()
	end
end
function shoot()
	local Bullet = Instance.new("Part", workspace)
	Bullet.Size = Vector3.new(0.3, 0.05, 0.1)
	Bullet.BrickColor = BrickColor.new("New Yeller")
	Bullet.Material = Enum.Material.Neon
	Bullet.Position = ShootPart.Position
	Bullet.Name = "Bullet"
	local Force = Instance.new("BodyVelocity")
	Bullet.CFrame = CFrame.new(mouse.hit.p) + Vector3.new(0,1,0)
	wait(2)
	Bullet:Destroy()
end

Script for damage

local Brick = workspace.Bullet
local function PlayerTouched(Part)
	local Parent = Part.Parent
	if game.Players:GetPlayerFromCharacter(Parent) then
		Parent.Humanoid.Health = Parent.Humanoid.Health - 10
	end
end

Brick.Touched:connect(PlayerTouched)

1 answer

It won't damage the npc because the game.Players:GetPlayerFromCharacter function checks if the character is a player's character, which obviously a custom npc is not. If you want your bullet to damage player's and custom npcs, then use:

if hit.Parent:FindFirstChild("Humanoid") then
	
end

I hope this helps :)

5 comments

0

it doesn't damage them, but it's because of this error 13:24:13.569 - Bullet is not a valid member of Workspace

0

(didn't mean to hit enter) but Bullet is in the Workspace

0

The script actually loads faster than the object so use Workspace:WaitForChild("Bullet")

0

i don't get the error, but it won't damage the npc/player still

0

should I make another post for this?

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