Trying to use the mouse to throw a grenade

As the title says, I’m trying to use where the players mouse is to throw a grenade

Currently what it’s doing is just dropping the grenade and killing the player, I’m wondering how i should make it so that the grenade gets to the position of the mouse, any help is appreciated because I’ve been wracking my brain for a while trying to solve this

local tool = script.Parent
local handle = tool.Handle
local debounce  = false
local BR = 10
local FT = 3
local BP = 100000

local mousePos

tool.Activated:Connect(function()
	
	
	tool.Throw.OnServerEvent:Connect(function(plr, mouseHit)

		mousePos = mouseHit
	end)
	if not debounce then
		debounce = true
		local grenade = handle:Clone()
		grenade.Parent = workspace


		grenade.CanCollide = true
		handle.Transparency = 1
		delay(FT,function()
			local explode = Instance.new("Explosion",workspace)
			explode.Position = grenade.Position
			explode.BlastRadius = BR
			explode.BlastPressure = BP
		end)
tool:Destroy()
	end
	

end)

Local script

local mouse = game.Players.LocalPlayer:GetMouse()
local tool = script.Parent
game:GetService("RunService").RenderStepped:Connect(function()


	tool.Throw:FireServer(mouse.Hit)
end)

image
this is what the grenade looks like

You shouldn’t listen to what im saying right now because im not too advanced yet but im pretty sure you need to use velocity.

local g = Vector.new(0,-workspace.Gravity,0)
local t = 1 -- time needed to reach the destination (in seconds)
local v0 = (FinishPos - StartPos - 0.5*g*t*t)/t -- Where FinishPos = MouseHit, StartPos = Character's HumanoidRootPart 
grenade.Velocity = v0

This script should work, adapt it to yours and give it a try!

would i put this in the local script or server script?


also got this error

did you define FinishPos and StartPos?

image

Sorry, I had made a mistake, StartPos = HumanoidRootPart’s Position, not the instance

StartPos = hRoot.Position

Still have the same error from before, heres the code now



local tool = script.Parent
local handle = tool.Handle
local debounce  = false
local BR = 10
local FT = 3
local BP = 100000
local g = Vector3.new(0,-workspace.Gravity,0)
local t = 1 -- time needed to reach the destination (in seconds)


local FinishPos
local StartPos
tool.Activated:Connect(function()
	local v0 = (FinishPos - StartPos - 0.5*g*t*t)/t -- Where FinishPos = MouseHit, StartPos = Character's HumanoidRootPart 
	tool.Velocity = v0
	tool.Throw.OnServerEvent:Connect(function(plr, mouseHit)
		local hRoot = plr.Character:FindFirstChild("HumanoidRootPart")
		FinishPos = mouseHit
		StartPos = hRoot.Position
	end)
	if not debounce then
		debounce = true
		local grenade = handle:Clone()
		grenade.Parent = workspace


		grenade.CanCollide = true
		handle.Transparency = 1
		
		delay(FT,function()
			local explode = Instance.new("Explosion",workspace)
			explode.Position = grenade.Position
			explode.BlastRadius = BR
			explode.BlastPressure = BP
		end)
tool:Destroy()
	end
	

end)```

You have to define FinishPos and StartPos before defining v0, not after

is the grenade unanchored, if so use CFrame.new(StartPos, MousePos) to make the grenade face towards the mouse position then set the velocity using by doing Velocity = grenade.CFrame.LookVector*speed

Probably just going to make the grenade another way because basically everything I’ve tried isnt working. Thanks for your help though

Got it figured out, had to do with the local script, if anyone else has this problem lmk and ill send hte script