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

can someone fix my coroutine?

Asked by 9 years ago

Please provide more explanation in your question. If you explain exactly what you are trying to accomplish, it will be much easier to answer your question correctly.
workspace.BlueGK.Value = 0 
workspace.RedGK.Value = 0 
local player = game.Players:GetChildren() 
TheCowsComeHome = nil
coroutine.wrap(function() repeat
for i = 1,#player do 
if player[i].className == "Player" and player[i].TeamColor == BrickColor.new("Deep blue") then 
workspace.BlueGK.Value = workspace.BlueGK.Value + 1 
wait(5)
workspace.BlueGK.Value = 0
end
end
wait(0.03)
until TheCowsComeHome end)()
for i = 1,#player do 
if player[i].className == "Player" and player[i].TeamColor == BrickColor.new("Bright red") then 
workspace.RedGK.Value = workspace.RedGK.Value + 1 
wait(5)
workspace.RedGK.Value = 0
end 
end
wait(0.0001)

please help me it doesnt work!!

0
Could you be more specific? connor12260311 383 — 9y
0
Make sure you give us your whole script. Also, please tab your script as well so we may easily read and help you. Also, explain the error, what is suppose to happen, and what is happening. Then we can assist you alphawolvess 1784 — 9y
0
What is this even supposed to do? BlueTaslem 18071 — 9y

1 answer

Log in to vote
0
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

I have given this advice before:

If you don't use coroutine.yield, you have no business using coroutines at all. Use spawn or delay if you just want to make a new "thread".

Also, tab your code correctly.

It's idiomatic to use while true do instead of repeat until nil. TheCowsComeHome is useless.


Only the Blue team is looked at repeatedly. Red is only looked at once. I'm guessing this is a mistake.


player is only defined once; thus the script will only know about the players that joined when it started running (if this is a normal script in ServerScriptService or the workspace, that means no one).

It also is a list of players, so you should really call it players. Since you only use it in to for i = 1, loops... you should probably just skip to using a generic for loop. I also recommend using :GetPlayers() instead of :GetChildren().


I don't know what this code is supposed to accomplish. I'm guessing you want to make RedGK and BlueGK have the number of players currently on that team. In that case there should be no waits in the loop over the players -- there shouldn't be time passing between handling individual players.

You should also make a function instead of repeating yourself for Blue and Red.

At this point your code would look like this:

workspace.BlueGK.Value = 0 
workspace.RedGK.Value = 0 

function CountTeam(team)
    local count = 0
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.TeamColor == team then
            count = count + 1
        end
    end
    return count
end


while true do
    workspace.BlueGK.Value = CountTeam( BrickColor.new("Deep blue") )
    workspace.RedGK.Value = CountTeam( BrickColor.new("Bright red") )
    wait(0.03)
end

No coroutine, only one function -- and it's a real one, and it's much shorter (and more correct).

A remark:

You don't really need BlueGK or RedGK for anything. It's just as reasonable to juse use CountTeam directly.

Ad

Answer this question