Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I stop a projectile from following its CFrame after it has been shot?

Asked by 5 years ago

I have a projectile I want to be launched forward from my character. Everything is good but the issue is that when I move my character a different direction after it has been shot the projectiles suddenly goes in that direction... Any ideas how to stop this here, is an example code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Debris = game:GetService('Debris')

local Remote = ReplicatedStorage.EG.FlickEvent
local Shockwave = ReplicatedStorage.EG:FindFirstChild('Shockwave')
local Ring = ReplicatedStorage.EG:FindFirstChild('Ring')
local Damage = 2

local ServerDebounces = {}

Remote.OnServerEvent:Connect(function(plr, Mouse)
 if not ServerDebounces[plr] then
    wait(.4)
  ServerDebounces[plr] = true

  local Char = plr.Character or plr.CharacterAdded:Wait()

  local SmashRing = Ring:Clone() 
  SmashRing.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,0,0)
  SmashRing.Parent = workspace

  local SmashClone = Shockwave:Clone()
SmashClone.CFrame = Char.HumanoidRootPart.CFrame * CFrame.new(0,1.1,-3)
  SmashClone.Size = Vector3.new(20,20,25)
  SmashClone.Parent = workspace
 local BodyVelocity = Instance.new("BodyVelocity")
  BodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
  BodyVelocity.Velocity = Vector3.new(0,0,0)
  BodyVelocity.Parent = SmashClone

 wait()
      for i = 0, 200 do
    wait()
    SmashClone.Orientation = SmashClone.Orientation + Vector3.new(0,0,5)
    BodyVelocity.Velocity = Char.HumanoidRootPart.CFrame.lookVector*i*5
    SmashRing.Size = SmashRing.Size + Vector3.new(0.2,0.2,0)*i
    SmashRing.Orientation = SmashRing.Orientation + Vector3.new(0,0,10)
    SmashRing.Transparency = SmashRing.Transparency - -0.01
    SmashClone.Transparency = SmashClone.Transparency - -0.01
    wait()
  ServerDebounces[plr] = false
  Debris:AddItem(SmashClone, 1.3)
  Debris:AddItem(SmashRing, 1)

 local Debounce = true
  SmashClone.Touched:Connect(function(h)
   if h.Parent:FindFirstChild('Humanoid') and h.Parent.Name ~= plr.Name and Debounce then  
    Debounce = false   
    local Enemy = h.Parent.Humanoid
    Enemy:TakeDamage(Damage)
    Debounce = false
    SmashClone:Destroy()
    wait(2)
    SmashRing:Destroy()
    Debounce = true
   end end)end end end)

0
It's a simple fix, take the lookVector as a local variable. You're constantly access the variable through the actual object, you aren't detaching it. GetGlobals 343 — 5y
0
BodyVelocity.Velocity = Char.HumanoidRootPart.CFrame.lookVector*i*5 << Make a variable called Char.HumanoidRootPart.CFrame.lookVector then do Var*i*5 GetGlobals 343 — 5y
0
Also make sure the variable is outside of the loop^^^^ GetGlobals 343 — 5y
0
Why can't he just summon the projectile infront of the character, and then the projectile's own lookVector as the means of travel? Fifkee 2017 — 5y
View all comments (2 more)
0
@BaseUrl Thank you! I feel silly to have not seen such a simple fix! It works perfectly! NFStudios 0 — 5y
0
If you would like you can add this as an answer so I am able to accept your answer ^^ NFStudios 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

I would suggest to delete the object after it has given damage.

Ad

Answer this question