Tools that aim
Implement a raycast projectile weapon using the aim position passed to Tool.Activated.
1. Create the tool
Insert RPG from the Toolbox, or create a tool with Tool on the ribbon. A server script inside the tool resolves script.Parent to the tool.
2. Projectile
Activated passes the player and the aim position:
local World = game:GetService("World")
local rpg = script.Parent
local reloading = {}
rpg.Activated:Connect(function(player, aim)
if reloading[player.UserId] then
return
end
reloading[player.UserId] = true
local character = player.Character
local start = character.Position + Vector3.new(0, 3.5, 0)
local look = character.LookVector
if aim and (aim - start).Magnitude > 1 then
look = (aim - start).Unit
end
local rocket = World:CreatePart()
rocket.Size = Vector3.new(0.8, 0.8, 0.8)
rocket.CanCollide = false
local at = start + look * 2
for step = 1, 100 do
local move = look * 3
local hit = World:Raycast(at, move)
if hit then
World:Explode(hit.Position, 10, 100, 600)
break
end
at = at + move
rocket.Position = at
task.wait()
end
rocket:Destroy()
task.wait(1.5)
reloading[player.UserId] = nil
end)Each frame advances the projectile 3 studs and raycasts the same segment. On a hit, World:Explode is called at the intersection point.
3. Configuration
The Toolbox RPG reads Damage, BlastRadius, BlastForce and ReloadTime from value objects inside its script:
World:Explode(hit.Position, setting("BlastRadius", 10), setting("Damage", 100), setting("BlastForce", 600))Melee variant
A melee tool ignores aim and queries Humanoids in front of the character:
local World = game:GetService("World")
local sword = script.Parent
sword.Activated:Connect(function(player)
local character = player.Character
local reach = character.Position + Vector3.new(0, 3, 0) + character.LookVector * 3
for _, humanoid in ipairs(World:GetHumanoidsNear(reach, 3.5)) do
if humanoid ~= character.Humanoid then
humanoid:TakeDamage(25)
end
end
end)See Tools and Tool for granting tools and the remaining events.