I am trying to learn vectors and physics on roblox studio so I decided to script a rocket launcher, but when i launch it there are 2 bugs.
1- the rocket kills the player that shot the rocket
2- the rocket spasms out and starts spinning while moving
here’s the script:
local tool = script.Parent
local Handle = tool.Handle
local rs = game:GetService("ReplicatedStorage")
local rocketEvent = rs.RocketServer
local swoosh = rs.PlaySwoosh
local rocketsound = tool:WaitForChild("Swoosh")
local boom = tool.Boom
local lifespan = 30
local rocketSpeed = 100 -- Constant speed of the rocket
swoosh.OnServerEvent:Connect(function(plr)
rocketsound.Looped = true
rocketsound:Play()
end)
rocketEvent.OnServerEvent:Connect(function(plr, raypos, raydist)
local Projectile = rs.ProjectilePart:Clone()
local gravity = workspace.Gravity
local projmass = Projectile:GetMass()
local CounterGrav = gravity * projmass
local att = Instance.new("Attachment")
att.Parent = Projectile
att.Name = "VectorAttachment"
local vf = Instance.new("VectorForce")
vf.RelativeTo = Enum.ActuatorRelativeTo.World
vf.Parent = Projectile
vf.Attachment0 = att
vf.Force = Vector3.new(0, CounterGrav, 0)
Projectile.Anchored = false
Projectile.Parent = workspace
local direction = (raypos - Handle.Attachment.WorldCFrame.Position).Unit
Projectile.AssemblyLinearVelocity = direction * rocketSpeed
Projectile.CFrame = CFrame.lookAt(Handle.Attachment.WorldCFrame.Position, raypos)
game:GetService("Debris"):AddItem(Projectile, lifespan)
Projectile.Touched:Connect(function(hit)
local exp = Instance.new("Explosion")
exp.Parent = workspace
exp.Position = hit.Position
rocketsound:Stop()
rocketsound.TimePosition = 0
boom:Play()
Projectile:Destroy()
end)
end)