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

Workspace.Tool.Handle.Script:7: attempt to index nil with 'Hit'?

I am trying to make a gun script but it gives this error Workspace.Tool.Handle.Script:7: attempt to index nil with 'Hit'. How do I fix this?

here is my code

local Tool = script.Parent.Parent local handle = script.Parent

Tool.Equipped:Connect(function()
	game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player)
		local Mouse = Player:GetMouse()
		local direction = (Mouse.Hit.p - handle.Position).Unit
		local Ray = Ray.new(handle.Position,direction)
		local Part = Instance.new("Part")
		Part.Parent = workspace
		Part.Anchored = true
		Part.Size = Ray.Direction - Ray.Origin
		Part.Size = Part.Size + Vector3.new(0.5,0.5,0.5)
	end)
end)

1 answer

You can also get the mouse from Tool.Equipped event. Try this:

local Tool = script.Parent.Parent
local handle = script.Parent

Tool.Equipped:Connect(function(Mouse)
	game.ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function()
		local direction = (Mouse.Hit.Position - handle.Position).Unit
		local Ray = Ray.new(handle.Position, direction)
		local Part = Instance.new("Part")
		Part.Parent = workspace
		Part.Anchored = true
		Part.Size = Ray.Direction - Ray.Origin
		Part.Size = Part.Size + Vector3.new(0.5,0.5,0.5)
	end)
end)

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