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

Code seems inefficient, can someone help catch rookie mistakes?

Asked by 5 years ago

I've been working on a game similar to The Conquerors 3. It's a strategy-game where units attack on their own and move when instructed to.

local tagging = game:GetService("CollectionService")
local target = script.Parent.Target
local soldier = script.Parent.Troop
tagging:AddTag(soldier,"unit")
while true do
    wait(0.1)
    if soldier.health > 0 and target.Value == nil then
        for index, unidad in pairs(_G.units) do
        if unidad.name == "Troop" and unidad.Health > 0 and
            unidad.Parent.TeamValue.Value ~= script.Parent.TeamValue.Value then
                local targetpos = unidad.Parent.Torso.Position
                if (targetpos - script.Parent.Torso.Position).magnitude <= 10 then
                    target.Value = unidad
                end
            end
        end
    end
end

The one above here is for searching through a list of soldiers tagged "unit". It then checks the team and distance. If it all checks out, the IntValue named "Target" inside the soldier object becomes "unidad". _G.units is a table full of soldiers tagged "unit" that is constantly updated by a script in ServerScriptStorage.

local target = script.Parent.Target
local soldier = script.Parent.Troop
target:GetPropertyChangedSignal("Value"):Connect(function()
    wait(0.3)
    while target.Value ~= nil and script.Parent.Ammo.Value > 0 do
        if target.Value.Health > 0 and (target.Value.Parent.Torso.Position - soldier.Parent.Torso.Position).magnitude <= 10 then
            soldier.AutoRotate = false
            soldier.Parent.Torso.CFrame =
            CFrame.new(soldier.Parent.Torso.Position,target.Value.Parent.Torso.Position)
            script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
            local random = math.random(1,1000)
            print(script.Parent.Ammo.Value)
            if random >= 300 then
                target.Value.Health = target.Value.Health - (15 - target.Value.Parent.Armor.Value)
            else
                target.Value.Health = target.Value.Health - 0
            end
        else
            target.Value = nil
            soldier.AutoRotate = true
        end
    wait(1)
end
    target.Value = nil
    soldier.AutoRotate = true
end)

This is the firing script. The ammo thing is for the soldier's ammo supply. The random.math is a hit or miss chance thing. The target.Health thing is to damage the enemy. The CFrame is to rotate and face the enemy.

P.S. I am trying to run 24 frames a second with 500 of these little soldiers on-screen. My PC has about 4 GB of RAM with 2 core things. Any optimization help would be appreciated!

Thanks for any help you can give!

0
If you are using humanoids, 500 on screen is wayyyyy too much. To have that many soldiers, you will have to build your own simplified rig. sleazel 1287 — 5y
0
the problem is not with the code; the problem is with the thing u want the code to do TheluaBanana 946 — 5y
0
Thanks, I disabled the character states and it seemed to help. I'l try to figure out I need and what I don't in a custom humanoid. Doglord120 0 — 5y

Answer this question