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)
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!
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)```
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