Hi, thank you for reading. What I'm trying to do is that when you change your team from citizens to prisoners, and you wait 30 seconds, your team will change into citizens again.
What I tried to do is insert a new IntValue to the person's who's team just got changed. After 30 seconds, when the timer is at 0, his team will automatically change into Citizens again.
The Problem is that this isn't working.
player = game.Players.LocalPlayer while true do wait(.1) for i,v in ipairs(game.Players:GetChildren()) do if Player[v].Team == Prisoner then local TeamTime = Instance.new("IntValue") TeamTime.Name = "TeamTimer" TeamTime.Value = 30 TeamTime.Parent = Player[v] if TeamTime.Value >= 30 then TeamTime.Value - 1 if TeamTime.Value == 0 then Player[v].Team = Citizen TeamTime:remove() end end end end end
If you would only like to see the working answer, go to the bottom. I wrote some analysis to help you understand the errors with your current program and how you could fix them if you want to script something like this using similar methods.
I would normally advise against giving the client the type of power you have given it in that local script; when code like that is run on the client it can lead to potential exploitation (since this means anyone with third party software can change teams just like your local script). This is why I changed the code to run on a server script in the one at the bottom.
While true do's take up more bandwidth than a listening event, which is why I used the listening event Changed to warn the script when the Team has switched for all players who have joined.
I personally prefer pairs over ipairs. As far as I can tell it doesn't seem to matter which is used since they both return an index and value. However, the value being returned if you go through the Player list is the USERDATA, not the string name of player. Player[v].Name would not work because of this. Since your wait(.1) you would have had to make TeamTime a NumberValue and make the value be subtraced by -.1 instead of -1. However, I completely replaced this method with a more efficient version, by using wait(30) and a relevance checker instead.
You never defined Player, so I don't really know what you're doing there, saying Player[v]
.
The if statement should have been
if TeamTime.Value >= 0 then TeamTime.Value = TeamTime.Value-.1 else v.TeamColor = game.Teams.Citizens.TeamColor end
Generally you want to avoid saying == whenever you're dealing with data values that aren't algebraic, because the way math works in binary for computers ends up changing number values because of how math ends up working out (you could get a number like .000000001 that's technically not equal to 0; you were using an IntValue though so that wouldn't have been a problem unless you converted to what I said before). You could always say if x < .001 and x >-.001 then
to avoid these mathematical glitches.
You are creating TeamTime EVERY time this while loop runs. There should be a conditional questioning whether there was already a TeamTime placed into the player, and then making that a local variable if it was, otherwise insert one.
Remove() is depreciated, use :Destroy() instead. It makes less lag as it deletes the object and descendants from memory in use COMPLETELY which saves the server bandwidth.
Place the code below in a SERVER script (not a local one). This code should be one of the more efficient methods for doing what you wanted to have accomplished, and allows for the use of filteringenabled which helps protect your game (provided you don't have other code on a localscript that manipulates the server directly).
game.Players.ChildAdded:connect(function(p) local totalYields = 0 p.Changed:connect(function(val) if p and p.Parent and val=="TeamColor" and p.TeamColor == game.Teams.Prisoners.TeamColor then totalYields = totalYields+1 local thisYield = totalYields wait(30) if thisYield == totalYields then p.TeamColor = game.Teams.Citizens.TeamColor end end end) end)