Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

can someone fix my coroutine?

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!!

3 comments

0

Could you be more specific?

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

0

What is this even supposed to do?

1 answer

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.