Looking for help with Vector math

Hello, I am trying to design a grenade-like system. Basically, wherever a player clicks, an explosion will be placed. I am using the humanoid target point of a tool as the position for the explosion.

The issue is, the explosion can happen anywhere you click, but I need to factor in range. I have tried many things, but they aren’t working. (If I could only place something at the end of a ray this would be super easy…).

Whenever I run this current version and throw an explosion out of range, the explosion occurs around the center of the map.

--calculate explosion's position within range
	local bombPos
	if (target - plr.Character.UpperTorso.Position).magnitude > gameSettings.BombThrowRange then
		bombPos = (-(target - plr.Character.UpperTorso.Position)).unit * gameSettings.BombThrowRange
	else
		bombPos = target
	end

Thanks!

Your issue is occurring because you are not adding back the position of the upper torso that you subtracted to get the relative offset of the target. If you change the first branch to something like

local upperTorsoPos = plr.Character.UpperTorso.Position
bombPos = (target - upperTorsoPos).Unit * gameSettings.BombThrowRange + upperTorsoPos

you should get the result you want.

Wow, that works perfectly! Thanks!