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

How do you change someone's team after 30 seconds?

Asked by
Yeevivor4 155
8 years ago

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

1 answer

Log in to vote
1
Answered by
Legojoker 345 Moderation Voter
8 years ago

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)
0
How long did it take you to write this? fahmisack123 385 — 8y
0
Maybe 15-20 minutes. Legojoker 345 — 8y
0
Too long for me. I wouldn't read this. Lol. Vezious 310 — 8y
0
Holy cow, thank you so much for the analysis and the code. I will use this and make sure that I never make these rookie mistakes ever again. Yeevivor4 155 — 8y
0
No problem! @Vezious Well you could always just take the bottom portion of the code and be on your merry way xD. Legojoker 345 — 8y
Ad

Answer this question