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

How to throw a brick at a player's direction?

local torso = Character.Torso -- pretend this is a player's character's torso
local part1 = Instance.new("Part", workspace)
part1.CFrame = torso.CFrame + Vector3.new(math.random(-20, 20), math.random(10, 20), math.random(-20, 20))

With this script, how do I make it so when the parts appear in the workspace they are thrown with force towards the character's torso?

1 answer

✓ Accepted answer

Here's something I typed up awhile back in the scripting helper's discord. It should answer your question. The only think you need to decide is the time it takes to reach the target.

img

-- example code
local function grenade(x0, x1, t)
	-- x1 = 0.5*g*t^2 + v0*t + x0
	local g = Vector3.new(0, -game.Workspace.Gravity, 0);
	local v0 = (x1 - 0.5*g*t^2 - x0) / t;
	local part = Instance.new("Part");
	part.CFrame = CFrame.new(x0);
	part.Shape = Enum.PartType.Ball;
	part.Size = Vector3.new(1, 1, 1);
	part.Velocity = v0;
	part.Parent = game.Workspace;
end

local mouse = game.Players.LocalPlayer:GetMouse();
local hrp = game.Players.LocalPlayer.Character:WaitForChild("HumanoidRootPart");
mouse.Button1Down:Connect(function()
	local x0 = (hrp.CFrame * CFrame.new(0, 0, -3)).p;
	local t = 1; --(mouse.Hit.p - x0) * 0.1; -- can adjust the time as you see fit
	grenade(x0, mouse.Hit.p, t);
end)

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