Attempt to index nil with "Hit"

So,

I’ve been trying to make a sticky bomb that when thrown sticks to players and objects and then explodes, dealing x amount of damage.

Things that I want to achieve:

  • Make the sticky bomb throwable.
  • Make the sticky bomb stick.
  • Allow players to throw the sticky bomb into the air (with a certain force).

How should I go about doing this with a tool?


I tried doing something today so the bomb could get thrown, but I got the error that the mouse was nil. What should I do?

function throwBomb(bombClone, mouse)
	local initialPos = bombHandle.Position
	local targetPos = mouse.Hit.Position
	local distance = (targetPos - initialPos).Magnitude
	
	local travelTime = 2
	local force = distance * Vector3.new(0, workspace.Gravity * 0.5, 0)
	
	bombClone:ApplyImpulse(force * bombClone.AssemblyMass)
end
local MOUSE_ICON = "rbxasset://textures/GunCursor.png" -- Changes the mouse to the GunCursor Icon.
local RELOADING_ICON = "rbxasset://textures/GunWaitCursor.png" -- Changes the mouse icon to the Gun Wait Curso Icon.

local player = game.Players.LocalPlayer

local Tool = script.Parent
local UserInputService = game:GetService("UserInputService")
local activateBombRemote = script.Parent:WaitForChild("ActivateBomb")

Tool.Equipped:Connect(function()
	UserInputService.MouseIcon = MOUSE_ICON
end)

Tool.Unequipped:Connect(function()
	UserInputService.MouseIcon = ""
end)

Tool.Activated:Connect(function()
	local cooldownTime = activateBombRemote:FireServer(player:GetMouse())
	
	repeat task.wait() until cooldownTime == false
	
	UserInputService.MouseIcon = RELOADING_ICON

	UserInputService.MouseIcon = MOUSE_ICON
end)

I reccomend watching this video, then just create a weld between the bomb and whatever it touches

2 Likes

To achieve your result, check out the documents below.
These are essential

Raycasting

Tools

You can’t pass the mouse to the server, it’s not replicated.
Try to pass the Hit instead.

In that case, can’t exploiters change the hit position?

Send the direction of the throw to the server, and cast a raycast (on server) to determine where it lands etc

To get the direction (on client) you can use Mouse.UnitRay.Direction

So would I use the mouse.unitray.direction for the direction parameter in the raycast and then have the origin be the handle’s position?

yes also multiply the direction in the raycast by a number like 100, the number you multiply the direction by would be the distance value, so it would look something like

local distance = 100
local raycastResult = workspace:Raycast(origin, direction * distance, raycastParams)
local position = raycastResult and raycastResult.Position