How should I go about making an object throwable

I have a local script that checks when the player presses their mouse which then sends over a remote event to the server script i have here

local FireThrown = game.ReplicatedStorage.RemoteEvents.FireThrown

FireThrown.OnServerEvent:Connect(function (plr, spell, RaycastPos, SpellPos, LookVector)
	local newSpell = spell:Clone()
	newSpell.Parent = game.Workspace

	local Fire = newSpell:FindFirstChild("Fire")
	local Sphere = newSpell:FindFirstChild("Sphere.001")

	if Fire and Sphere then
		Sphere.HandleWeld:Destroy()
		newSpell:PivotTo(CFrame.new(SpellPos))

	end

end)

Its a fireball, and i want it to go in the direction the players mouse goes. I also want it to explode upon impace, how should i go about this using this script?

you’ll need to use a LookVector.

local playerMouse = game.Players.LocalPlayer:GetMouse()

local directionVector = Mouse.Hit.LookVector

Using ShapeCast, cast a sphere that is the size of the fireball along the direction given by the mouse. Tween the position of the fireball from the player to the end position of the shapecast, and when the tween ends, do the explosion.

edit: if a player / object gets in the way, you can have a touched event or use GetPartsInPart() every heartbeat to cancel the tween and fire the explosion quicker.

1 Like

what if the player was looking up in the air when it happened?

You can use:

Mouse.UnitRay.Direction

to get the direction of the mouse relative to the camera, then ApplyImpulse the part you want to throw with the direction vector multiplied by a scaling factor.

Although to add onto sick_tr1cks reply, I believe Mouse.Hit will return a valid CFrame irrespective of whether there’s a Mouse.Target.

You could take a unit vector between the hit position and the character: (Hit.Position -HumanoidRootPart.Position).Unit if you want the object thrown towards the mouse from the perspective of the character.

Also fyi, while the Mouse object is not yet deprecated, it’s been ‘superseded’ by UserInputService. This doesn’t mean much, but keep an eye on whether Roblox decides one day to get rid of it in which case I believe the established method is to cast your own ray from the Camera using ViewPortToRay and retrieve the values that way. Please correct me someone if I’m wrong with this, I’ve been out of the loop of Roblox changes for the past few years.

1 Like

To add even further, ApplyImpulse will throw it, but it will still be affected by Gravity. If you want it to travel in a straight line then consider using a LinearVelocity.

local FireThrown = game.ReplicatedStorage.RemoteEvents.FireThrown
local worldRoot = game:GetService("Workspace")


FireThrown.OnServerEvent:Connect(function (plr, spell, RaycastPos, SpellPos, LookVector)
	local newSpell = spell:Clone()
	newSpell.Parent = game.Workspace

	local Fire = newSpell:FindFirstChild("Fire")
	local Sphere = newSpell:FindFirstChild("Sphere.001")

	if Fire and Sphere then
		Sphere.HandleWeld:Destroy()
		newSpell:PivotTo(CFrame.new(SpellPos))


		newSpell.Fire:ApplyImpulse(LookVector * 100)
	end

end)

now I’ve got this script but there’s a little bit of delay in between the impulse that makes it seem not very smooth, is there any way i could fix that?

actually, scratch that, how could i do this with LinearVelocity instead? i can’t seem to figure out how to use it.