generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Need help making this more efficient

Previous Thread :: Next Thread 
Raildex is not online. Raildex
Joined: 06 Dec 2009
Total Posts: 934
09 Mar 2016 02:57 PM
So I have made this script that finds a target and fires a bullet, This is very resource demanding from what I can tell and need to make it more efficient. Can you guys look through this and see if you can think of any ways that this could be more efficient/ cut down on proccessing? (The findNearestTorso function is basically repeated checking through all team ships for a target, its long but not complex) The while loop is wht takes up the most resources from what I can tell.



function findNearestTorso(pos)
local list
local torso = nil
local dist = 40
local temp = nil
local human = nil
local temp2 = nil
local found = false


if found == false then
if team ~= "Valtek Empire" then
list = game.Workspace["Valtek Empire"].Ships:children()
for x = 1, #list do
temp2 = list[x] -- gets the list of ships within this empire
if found == false then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("ShipHumanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and temp.BrickColor ~= script.Parent.Torso.BrickColor then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
found = true
end
end
end
end
end
end

if found == false then
if team ~= "Baris Republic" then
list = game.Workspace["Baris Republic"].Ships:children()
for x = 1, #list do
temp2 = list[x] -- gets the list of ships within this empire
if found == false then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("ShipHumanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and temp.BrickColor ~= script.Parent.Torso.BrickColor then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
found = true
end
end
end
end
end
end

if found == false then
if team ~= "Corian Alliance" then
list = game.Workspace["Corian Alliance"].Ships:children()
for x = 1, #list do
temp2 = list[x] -- gets the list of ships within this empire
if found == false then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("ShipHumanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and temp.BrickColor ~= script.Parent.Torso.BrickColor then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
found = true
end
end
end
end
end
end

if found == false then
if team ~= "Reltek Union" then
list = game.Workspace["Reltek Union"].Ships:children()
for x = 1, #list do
temp2 = list[x] -- gets the list of ships within this empire
if found == false then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("ShipHumanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and temp.BrickColor ~= script.Parent.Torso.BrickColor then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
found = true
end
end
end
end
end
end

if found == false then
if team ~= "Seraph Federation" then
list = game.Workspace["Seraph Federation"].Ships:children()
for x = 1, #list do
temp2 = list[x] -- gets the list of ships within this empire
if found == false then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("ShipHumanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and temp.BrickColor ~= script.Parent.Torso.BrickColor then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
found = true
end
end
end
end
end
end

if found == false then
if team ~= "Sovereign Systems" then
list = game.Workspace["Sovereign Systems"].Ships:children()
for x = 1, #list do
temp2 = list[x] -- gets the list of ships within this empire
if found == false then
temp = temp2:findFirstChild("Torso")
human = temp2:findFirstChild("ShipHumanoid")
if (temp ~= nil) and (human ~= nil) and (human.Health > 0) and temp.BrickColor ~= script.Parent.Torso.BrickColor then
if (temp.Position - pos).magnitude < dist then
torso = temp
dist = (temp.Position - pos).magnitude
found = true
end
end
end
end
end
end


return torso
end


while true do
wait(.01)

for i = 1, 4 do
wait(.02)
if target == nil or target.Parent.ShipHumanoid.Health <= 0 then
target = findNearestTorso(script.Parent.Head.Position)
end
local turret = script.Parent["turret"..i]

if target ~= nil then
print("target exists")
if (script.Parent.Head.Position - target.Position).magnitude < 40 then
if turnToEnemy == true then
script.Parent.Torso.CFrame = CFrame.new(script.Parent.Torso.Position, target.Position)
end
print("found ship in range")
if bulletType == "bullet" then
local randX = math.random(-20,20)/10
local randY = math.random(-10,10)/10
local randZ = math.random(-20,20)/10
print(randX..randY..randZ)

print("in range")
local bullet = Instance.new('Part', game.Workspace)

game.Debris:AddItem(bullet, 2)

bullet.BrickColor = BrickColor.new("Bright blue")
bullet.Material = "Neon"
bullet.Transparency = 0.2
bullet.CanCollide = false
bullet.Size = Vector3.new(.2,.2, 2)
bullet.CFrame = turret.CFrame
bullet.CFrame = CFrame.new(bullet.Position,Vector3.new(target.Position.X+randX,target.Position.Y+randY,target.Position.Z+randZ))
local v = Instance.new("BodyVelocity", bullet)
v.velocity = bullet.CFrame.lookVector * bulletSpeed
v.maxForce = Vector3.new(math.huge, math.huge, math.huge)
v.P = 5000
print("bullet fired")


bullet.Touched:connect(function(obj)
local humanoid = obj.Parent:FindFirstChild'ShipHumanoid' --or obj.Parent.Parent:FindFirstChild'ShipHumanoid'

if humanoid and obj.Parent.Torso.BrickColor ~= script.Parent.Torso.BrickColor then

humanoid.Health = humanoid.Health - damage.Value
local hitBrick = Instance.new('Part',game.Workspace)
if pierce == false then
bullet:destroy()
end
game.Debris:AddItem(hitBrick, 1)
hitBrick.BrickColor = BrickColor.new("Toothpaste")
hitBrick.Material = "Neon"
hitBrick.Transparency = .8
hitBrick.CanCollide = false
hitBrick.Anchored = true
hitBrick.Shape = "Ball"
hitBrick.Size = Vector3.new(1.2+randY,1.2+randY,1.2+randY)
hitBrick.CFrame = bullet.CFrame
wait(.07)
hitBrick.Transparency = .84
wait(.07)
hitBrick.Transparency = .88
wait(.07)
hitBrick.Transparency = .92
wait(.07)
hitBrick.Transparency = .96
wait(.07)
hitBrick:remove()


if humanoid.Health <= 0 then
--target = findNearestTorso(script.Parent.Torso.Position)
end
end
end)
wait(randY/2)
end
else
target = findNearestTorso(script.Parent.Head.Position)
end
else
target = findNearestTorso(script.Parent.Head.Position)
end
end--for loop end


end
Report Abuse
Raildex is not online. Raildex
Joined: 06 Dec 2009
Total Posts: 934
09 Mar 2016 07:16 PM
7hr Bump

Will no one dare attempt to conquer this beast?
Report Abuse
Raildex is not online. Raildex
Joined: 06 Dec 2009
Total Posts: 934
09 Mar 2016 11:21 PM
11 hr bump cmon guys
Report Abuse
Lykaon is not online. Lykaon
Joined: 27 Oct 2014
Total Posts: 784
09 Mar 2016 11:31 PM
teams = {
"valtek emperer",
"duh",
"blah"
}

i think u can get somewhere with this
Report Abuse
izzatnasruna is not online. izzatnasruna
Joined: 09 Jun 2009
Total Posts: 1927
09 Mar 2016 11:33 PM
Why are you even asking us?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image