Hi, so I have a homing attack script and I am trying to set out targets for sonic to destroy, but my problem is that sonic can't aim his attack at the targets at all and I have no idea why!!!
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local Fall = false
local charging = false
local homing = false
local speed = 30
local direction = Vector3.new(0, 0, 1)
local lastPosition = Vector3.new(0, 0, 0)
local orginPosition = Vector3.new(0, 0, 0)
local lastTarget
local velocity = Instance.new("BodyVelocity")
local spin = Instance.new("Part")
spin.Name = "Spin"
spin.Anchored = true
spin.CanCollide = false
spin.BackSurface = 0
spin.BottomSurface = 0
spin.FrontSurface = 0
spin.LeftSurface = 0
spin.RightSurface = 0
spin.TopSurface = 0
spin.Shape = 0
spin.Material = "Neon"
spin.Size = Vector3.new(4, 4, 4)
spin.Transparency = 1
mesh = Instance.new("SpecialMesh")
mesh.Parent = spin
mesh.Scale = Vector3.new(0.05, 0.05, 0.05)
mesh.MeshId = "rbxassetid://489671777"
mesh.TextureId = "rbxassetid://489667787"
mesh.Offset = Vector3.new(0, -0.5, 0)
ParticleEmitter = Instance.new("ParticleEmitter")
ParticleEmitter.Texture = "http://www.roblox.com/asset/?id=249481494"
ParticleEmitter.LockedToPart = true
local hint = Instance.new("Hint")
hint.Text = "Spin Dash: Click and release | Homing attack: Double press spacebar"
function checkSit()
if player.Character == nil then
return true
end
if player.Character:FindFirstChild("Humanoid") == nil then
return true
end
if player.Character.Humanoid.Sit == true then
return true
end
return false
end
function transparent(value, object)
if object == nil then
return
end
object = object:children()
if #object < 1 then
return
end
for i = 1, #object do
if object[i]:IsA("BasePart") and object[i].Name ~= "Spin" and object[i].Name ~= "HumanoidRootPart" then
object[i].Transparency = value
else
transparent(value, object[i])
end
end
end
function onButton1Down(key)
if key == "e" then
local torso = player.Character:FindFirstChild("Torso")
if charging == true or torso == nil or homing == true or checkSit() == true then
return
end
charging = true
speed = 50
spin.BrickColor = torso.BrickColor
spin.Parent = player.Character
for i = 1, 50 do
if charging == false then
return
end
if Fall == true then
break
end
speed = speed + 3
spin.CFrame = CFrame.new(Vector3.new(0, 0, 0), direction) + torso.Position - Vector3.new(0, 1.01, 0)
spin.CFrame = spin.CFrame * CFrame.fromEulerAnglesXYZ(-1.7 * i, 0, 0)
if spin.Parent == nil then
spin.Parent = player.Character
end
wait()
if i == 1 then
transparent(1, player.Character)
end
end
if Fall == true then
transparent(1, player.Character)
end
onButton1Up()
end
end
function onButton1Up(key)
if key == "e" then
local torso = player.Character:FindFirstChild("Torso")
if torso == nil or Fall == true or charging == false or checkSit() == true then
return
end
charging = false
lastPosition = torso.Position
velocity.maxForce = Vector3.new(100000, 0, 100000)
velocity.Parent = player.Character:FindFirstChild("Torso")
for i = 1, speed do
if Fall == true or charging == true then
break
end
if checkSit() == true then
break
end
speed = speed - 1
local trail = spin:clone()
trail.Transparency = 0.6
trail.TopSurface = 0
trail.BottomSurface = 0
trail.CFrame = spin.CFrame
trail.Parent = player.Character
mesh = Instance.new("SpecialMesh")
mesh.Parent = trail
mesh.Scale = Vector3.new(0.04, 0.04, 0.04)
mesh.MeshId = "rbxassetid://464790010"
mesh.TextureId = "rbxassetid://476262394"
mesh.Offset = Vector3.new(0, -0.5, 0)
ParticleEmitter = Instance.new("ParticleEmitter")
ParticleEmitter.Texture = "rbxassetid://249481494"
game:service("Debris"):addItem(trail, 0.15)
spin.CFrame = CFrame.new(Vector3.new(0, 0, 0), direction) + torso.Position - Vector3.new(0, 1.01, 0)
spin.CFrame = spin.CFrame * CFrame.fromEulerAnglesXYZ(-1.7 * i, 0, 0)
if spin.Parent == nil then
spin.Parent = player.Character
end
velocity.velocity = direction * speed
lastPosition = torso.Position
if i == 1 then
transparent(0, player.Character)
end
wait()
end
if Fall == false or charging == false then
velocity.Parent = nil
spin.Parent = nil
end
end
end
function onKeyDown(key)
key = key:lower()
local torso = player.Character:FindFirstChild("Torso")
if key == " " and Fall == false and torso ~= nil then
Fall = true
spin.BrickColor = torso.BrickColor
for i = 1, 15 do
if homing == true or Fall == false then
break
end
spin.CFrame = torso.CFrame * CFrame.fromEulerAnglesXYZ(-0.5 * i, 0, 0)
if spin.Parent == nil then
spin.Parent = player.Character
end
wait()
if i == 2 then
end
end
if homing == false then
spin.Parent = nil
end
Fall = false
elseif key == " " and homing == false and torso ~= nil then
homing = true
Fall = false
local target
local distance = 80
local list = game.Workspace:children()
for i = 1, #list do
if list[i].className == "Part" and list[i].Size.magnitude < 1 and list[i] ~= lastTarget and distance > list[i].Position - torso.Position.magnitude and list[i].Position.y < torso.Position.y then
target = list[i]
distance = list[i].Position - torso.Position.magnitude
end
end
if target ~= nil then
lastTarget = target
velocity.maxForce = Vector3.new(100000, 100000, 100000)
velocity.Parent = torso
for i = 1, 50 do
if Fall == false or torso.Position - target.Position.magnitude < 4 then
break
end
spin.CFrame = torso.CFrame * CFrame.fromEulerAnglesXYZ(-1 * i, 0, 0)
if spin.Parent == nil then
spin.Parent = player.Character
end
local trail = spin:clone()
trail.Transparency = 1
trail.TopSurface = 0
trail.BottomSurface = 0
trail.CFrame = spin.CFrame
trail.Parent = player.Character
mesh = Instance.new("SpecialMesh")
mesh.Parent = spin
mesh.Scale = Vector3.new(0.04, 0.04, 0.04)
mesh.MeshId = "rbxassetid://464790010"
mesh.TextureId = "rbxassetid://484181635"
mesh.Offset = Vector3.new(0, -0.5, 0)
ParticleEmitter = Instance.new("ParticleEmitter")
game:service("Debris"):addItem(trail, 0.5)
velocity.velocity = target.Position - torso.Position.unit * 80
wait()
end
spin.Parent = nil
transparent(0, player.Character)
homing = false
velocity.maxForce = Vector3.new(0, 100000, 0)
velocity.velocity = Vector3.new(0, 50, 0)
wait(0.2)
velocity.Parent = nil
else
local originPosition = torso.Position
lastTarget = nil
velocity.maxForce = Vector3.new(100000, 100000, 100000)
velocity.Parent = torso
for i = 1, 14 do
if torso.Position.y - originPosition.magnitude > 14 or checkSit() == true then
break
end
spin.CFrame = torso.CFrame * CFrame.fromEulerAnglesXYZ(-1 * i, 0, 0)
if spin.Parent == nil then
spin.Parent = player.Character
end
local trail = spin:clone()
trail.Transparency = 0.8
trail.TopSurface = 0
trail.BottomSurface = 0
trail.CFrame = spin.CFrame
trail.Parent = player.Character
mesh = Instance.new("SpecialMesh")
mesh.Parent = trail
mesh.Scale = Vector3.new(0.04, 0.04, 0.04)
mesh.MeshId = "rbxassetid://464790010"
mesh.TextureId = "rbxassetid://484181635"
mesh.Offset = Vector3.new(0, -0.5, 0)
game:service("Debris"):addItem(trail, 1)
velocity.velocity = torso.CFrame.lookVector * 50
wait()
end
velocity.Parent = nil
spin.Parent = nil
wait(0.25)
homing = false
end
end
end
if mouse == nil then
return
end
if player:FindFirstChild("Message") ~= nil then
player.Message.Parent = nil
end
hint.Parent = player
mouse.KeyDown:Connect(onButton1Down)
mouse.KeyUp:Connect(onButton1Up)
mouse.KeyDown:Connect(onKeyDown)
mouse.Move:Connect(function()
if player.Character:FindFirstChild("Torso") ~= nil then
direction = mouse.Hit.p - player.Character:FindFirstChild("Torso").Position.unit
direction = Vector3.new(direction.x, 0, direction.z)
end
end)

1 comment
You forgot to put in the parent of the variable spin, so I think you would like to do spin.Parent = game.Workspace