Help with gun script

I’m trying to make a script for a tool which fires a part that insta kills anyone who get hits by it. Everything works fine apart from the kill script. For some reason an error message is produced reading “Humanoid is not a valid member of MeshPart”. I have no idea why this is the case.

local repStor = game:GetService("ReplicatedStorage")
local bullet = repStor.Part

local Tool = script.Parent
local tolly = script.Parent.Handle

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local newBullet = bullet:Clone()


Tool.Activated:Connect(function()
	newBullet.Position = tolly.Position
	newBullet.Parent = game.Workspace
	newBullet.Velocity = mouse.Hit.LookVector * 1000
end)

newBullet.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Humanoid.Health = 0
	end
end)
1 Like

You are doing hit.Parent:FindFirstChild and then you are doing hit.Humanoid so I think maybe you want to do hit.Parent.Humanoid?

1 Like

Yeah, thanks. Can’t believe I missed that.

Adding onto what ryan said you also might want to add a debounce so your .Touched function isn’t running several times when it would only need to run once for that specific player.

Read this to understand more about debounces.

1 Like

I’ll look into that. Thanks for the advice. I’m pretty new to scripting so any ways to improve my code are always good.

Good idea, you should use a debounce and wait maybe like 1 second. Not sure.

1 Like