I am trying to make it so if you are on a certain team, ("Bright blue") the script will wait() then give you cash. Every time I have tried doing that it breaks the leaderboard. Can someone help?
function onPlayerEntered(newPlayer) local stats = Instance.new("IntValue") stats.Name = "leaderstats" local points = Instance.new("IntValue") points.Name = "Currency" points.Value = 0 stats.Parent = newPlayer points.Parent = stats while true do wait(5) points.Value = points.Value +50 end end game.Players.ChildAdded:connect(onPlayerEntered)
Your while loop is not exiting. While the loop is running nothing outside of it will be run until the loop terminates. A while loop can terminate when the condition becomes false (since you're using while true
do it will never terminate), or if you use the keyword break
.
For your intended purposes, you're not wanting to exit the while loop, so we will want to move the while loop. The while loop will need to go after lines 14 and 15 in your code in order to add the stats to the leaderboard.
I have added the comparing of team colors to the script. How the script now works is if the player is ever on the team with a TeamColor of Bright blue, then the player will be awarded five points. Since TeamColors are BrickColor values, you will need to compare the TeamColor with a BrickColor. That is why the script has newPlayer.TeamColor == BrickColor.new('Bright blue')
.
function onPlayerEntered(newPlayer) local stats = Instance.new("IntValue") stats.Name = "leaderstats" local points = Instance.new("IntValue") points.Name = "Currency" points.Value = 0 stats.Parent = newPlayer --These just needed to be moved, otherwise they will never be reached on the other side of the while loop. points.Parent = stats while true do if newPlayer.TeamColor == BrickColor.new('Bright blue') then --If the player's team color is Bright blue, then we'll give them points. Lua is case sensitive, so make sure you have the color set properly. TeamColors are BrickColor values, so you need to compare all BrickColor values with another BrickColor value. wait(5) --Wait 5 seconds before giving points. points.Value = points.Value + 50 end end end game.Players.PlayerAdded:connect(onPlayerEntered) --I would recommend using PlayerAdded as there may be objects added to Players that are not necessarily a player.
Hello iFurzy,
My Word On This I do not think that you can directly give any type of stats to a team. However, you can give it evenly among a team.
What Is Wrong With Your Script The problem is that you are actually adding the leaderboard after the while loop. And that loop is not going to end.
Your Script Corrected
function onPlayerEntered(newPlayer) local stats = Instance.new("IntValue") stats.Name = "leaderstats" local points = Instance.new("IntValue") points.Name = "Currency" points.Value = 0 stats.Parent = newPlayer points.Parent = stats while true do wait(5) points.Value = points.Value +50 end end game.Players.ChildAdded:connect(onPlayerEntered)
If you are satisfied please Upvote and Accept Answer
Thanks, RadioactiveSherbet