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

How Do You Give Cash To A Team Overtime?

Asked by
iFurzy 35
7 years ago
Edited 7 years ago

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)

2 answers

Log in to vote
2
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
7 years ago

Problem

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.


Solution

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.


Additional Information

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').


Final Script

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.

Hopefully this answered your question, if so do not forget to hit the accept answer button. If you have another other questions, feel free to comment below.
Ad
Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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

0
Yep, @M39a9am3R did it way better RadioactiveSherbet 30 — 7y
0
So basically you want to continually change the parent the parent of the leaderstats/int values DeveloperSolo 370 — 7y
0
Thanks @JohnTUnderwood for catching my problem. That was a mistake :D RadioactiveSherbet 30 — 7y

Answer this question