Raycasting Help / Gun Help?

Hi. I’m trying to make a Gun for my Game. And i’m trying to make so that if the Ray hits the Head. The Player Will Die Immediately. Here is the Script. Any help is Appreciated. And thanks for reading this!:

local TheLocalPlayer = game.Players.LocalPlayer
local TheLocalPlayersMouse = TheLocalPlayer:GetMouse()
local TheTool = script.Parent
local TheBoolValue = script.Parent.Value

TheTool.Activated:Connect(function()
	if TheBoolValue.Value == true then
		TheBoolValue.Value = false
		script.Parent.Sound_1:Play()
		local TheNewRay = Ray.new(TheTool.Handle.CFrame.p, (TheLocalPlayersMouse.hit.p - TheTool.Handle.CFrame.p).Unit * 500)
		local Hit, Position = game.Workspace:FindPartOnRay(TheNewRay)
		if Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
			local TheHumanoid = Hit.Parent:FindFirstChild("Humanoid")
			TheHumanoid:TakeDamage(29)
		elseif Hit.Name == "Head" then
			local TheHumanoid = Hit.Parent:FindFirstChild("Humanoid")
			TheHumanoid.Health = 0 
			print("HeadShot!?")
		else
			local TheBulletHole = game.ReplicatedStorage.Part:Clone()
			TheBulletHole.Parent = game.Workspace
			TheBulletHoleCFrame = CFrame.new(Position)
		end
		wait(5)
		TheBoolValue.Value = true
		print(199)
	end
end)
2 Likes

I’m confused why you would add The into the variable names. So I butchered your variables and fixed the logic error.

local LocalPlayer = game.Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
local Tool = script.Parent
local BoolValue = script.Parent.Value

Tool.Activated:Connect(function()
	if BoolValue.Value == true then
		BoolValue.Value = false
		script.Parent.Sound_1:Play()
		
		local NewRay = Ray.new(Tool.Handle.CFrame.p, (LocalPlayersMouse.hit.p - Tool.Handle.CFrame.p).Unit * 500)
		local Hit, Position = game.Workspace:FindPartOnRay(NewRay)
		if Hit.Parent and Hit.Parent:FindFirstChild("Humanoid") then
			local Humanoid = Hit.Parent:FindFirstChild("Humanoid")
			Humanoid:TakeDamage(29)
			if Hit.Name == "Head" then
				Humanoid.Health = 0
				print("Headshot!")
			end
		else
			local BulletHole = game.ReplicatedStorage.Part:Clone()
			BulletHole.Parent = game.Workspace
			BulletHoleCFrame = CFrame.new(Position)
		end
		
		wait(5)
		BoolValue.Value = true
		print(199)
	end
end)
1 Like

Idk. It’s just a Habit i do. And thanks I’m going to see if this works!

image

Who’s TheLocalPlayer? These variables are also really confusing - why is the Tool script.Parent, but a BoolValue is script.Parent.Value (unless the BoolValue is named Value)? As well, if script.Parent is assigned to Tool, why do more script.Parent?

I only mangled the script a little, nothing too big that will fix it. Unfortunately, I’m really limited on resources and low on energy to actually put additional effort.


First issue fixed. I assume that script.Parent.Value leads to a BoolValue.

Oh, I thought that was a genuine and thorough fix effort. It’d be up to the OP to clean that up then. I was just confused at some of the choices in the code so I had to ask.