Playtime Reward

Im looking to give a Player +1 item every 60 seconds.

while true and task.wait(1) do
     playerItem += 1
end

^ this would be on the PlayerJoined connection

My question is:
Would this loop giving method be efficient for a larger scale of players?

3 Likes

you can add to all larger scale of players.when the event runs the function for too much time,every function will work since they started.

No problem should appear :slight_smile:

3 Likes

The players.PlayerAdded() event spawns a new task on the server every time it is called. The loop would only run in 1 task, meaning that it would be fine if multiple players join. There might be issues with performance if there are large amounts of players on each server, but it shouldn’t cause serious lag.

Also, just something I noticed, doing:

while true and task.wait(1) do

The true part of that line is redundant. You can instead just do:

while tast.wait(1) do

And it would work the same.

5 Likes

Wait pls answer my wondered question :sob:

while true do
    task.wait(1)
end

OR

while task.wait(1) do

end

which one is better i commonly use the first one please help :sob:

2 Likes

I’ve seen a good amount of people say that task.wait(1) is more efficient
never have I done any sort of tests to prove this myself but i just got into habit to use task.wait()
:slight_smile:

this deserves to be shamed for i cannot read to save my life LOL

1 Like

This is better. It takes up less space and does the exact same thing.

1 Like

Oh man i thought the first one was better :frowning:

ok i’ll try this :smiley:

1 Like

ignore me for i was dyslexic there :skull: LOL
anyways thanks for commenting :smiley: <3

1 Like

yes sure use task.wait(1) i tested difference between wait() and task.wait() wait is not waiting correctly.something like 1.2 seconds but task.wait(1) actually waits like 1.0 seconds

wait() will be get deprecated i must cry for that :rage:

2 Likes

You should use the first option if you have a conditional statement, in place of “true”, that will be false at some point. Otherwise, as @Fittergem1000 stated, you should just use the second option.

1 Like

You should do

while true do
for i,v in pairs(game.Players:GetPlayers()) do
pcall(function())
     v.playerItem += 1
end)
end
end

in a server script. This is the easiest and most effective way to reward all players equally for playing.

1 Like

uh hey but we will add every player at same time for this code?

and uhh.not the best way what if it triggers when player isnt loaded fully?

1 Like

Correct, if you are only looping something that if a value is true, EX:

local value = false

while value = true do

Then you should use the first option. Otherwise the second object saves space and time.

1 Like

no this code is all you need. You don’t need to keep track of the current players in the server as game.Players:GetPlayers() does that for you

1 Like

how would this be equal playing time.
no hate.

just curious on your thinking

this would mean if i join like .2 seconds before the loop plays then ill get that reward others worked for.

2 Likes

that one sucks my script wasnt working because of that little error.

if value is false then the loop becomes mad and goes to MCdonalds and never comes back :rage:

1 Like

im not choosing favotires for the solution I chose just wanted to say this cause you also helped the other persons response is helpful for more people since its a bit more in depth answer
<3 thanks for respond

1 Like

there is no other solution that is “more equal” than the one I provided.

1 Like

but if the playerjoined makes a new task each time its called then you would wanna reward for players ACTUAL playtime vs a global one no?

1 Like

Your solution provides a loop for a global reward system. The system that @bbnoshanky has in place is fired upon a player joining (players.PlayerAdded()). The player is already defined in the function and the server is only managing the specific player that joined. This happens every time a new player joins, that way each player gets their own reward loop.