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

script is looping correctly but has to loop extremely fast. is there a better way to loop?

Asked by 2 years ago

So in my game theres a certain round that will spawn different types of enemie, and when they spawn in they add +1 to the value "DogsLeft". When they die they subtract the "DogsLeft" value by -1. So i made this script so when theres 1 zombie left, it enables other scripts within the model. heres the code:

while true do
    wait(0.1)
    if game.ReplicatedStorage.Values.DogsLeft.Value == 1 then
        script.Parent.Drop4.Disabled = false
        script.Parent.Death2.Disabled = false
        script.Parent.Death.Disabled = true
    else
        --print "dog round still active"
    end 
end

so it works fine, but i really don't like having a script loop 0.1 seconds, but the issue is if i have it at 1 or 2 seconds, it'll cause a problem where if the final enemies are being killed and the script hasnt looped, it wont enable the scripts because the script would read the value as jumping from (for example) 3 to 0 within those 1 - 2 seconds, skipping the values between

is there a better way to loop this script? or a different way to loop scripts?

0
use RenderStepped, it runs on every frame greatneil80 2647 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

A good alternative is to use event based programming, or module scripts. Module scripts are a great way to communicate server-server without enabling/disabling scripts like you are doing. It would only be a matter of checking how many dogs are left when a dog dies, in say 'DogHandler', and then when there's only one left 'DogHandler' will run a function inside some other module script to run whatever code needs to be run. I suggest you spend some time learning about module scripts to get an idea of what I mean, I can promise you that they are 100% worth the effort as they make your life infinitely easier in any decently sized game.

Single-script architecture would also be worth checking out, as it goes hand-in-hand with module scripts.

0
Will research this! nick2222 20 — 2y
Ad

Answer this question