#Better way to write this script?

28 messages · Page 1 of 1 (latest)

grim cosmos
#

Okay so a while ago I made this script that counts the player's playtime, but it's very bad for server performance, any ideas on how to make it more efficient?

game.Players.PlayerAdded:Connect(function(plr)
    task.wait(5)
    local val = plr:WaitForChild("leaderstats"):WaitForChild("Playtime")
    while task.wait(1) do val.Value += 1 end
end)

Would it be more efficient to do something like:

while task.wait(1) do
  for i,v in game.Players:GetChildren() do
    v:WaitForChild("leaderstats"):WaitForChild("Playtime").Value += 1
  end
end
rapid lichen
#
game.Players.PlayerAdded:Connect(function(plr)
    task.wait(5)
    local val = plr:WaitForChild("leaderstats"):WaitForChild("Playtime")
    while task.wait(1) do val.Value += 1 end
end)
#

so first off

#

while true do

#

isnt really

#

needed

#

you can just add runservice and add a debounce

#

like

#
local debounce = false

put a runservice.renderstepped here

if debounce then return end

debounce = true

val.Value += 1

wait(AMOUNT)

debounce = false

end
#

that isnt exactly the code since i dont like spoonfeeding

#

nice job on the task.wait

#

thats good

#

but

#

implement that instead of while task.wait(1) do

#

while true do is legit out of date, never use it unless you HAVE to

#
while task.wait(1) do
  for i,v in game.Players:GetChildren() do
    v:WaitForChild("leaderstats"):WaitForChild("Playtime").Value += 1
  end
end
#

now for this

#

once again

#

while true do

#

dont need it

rapid lichen
#

and

#

replace while true do with it

#

and then youre done

#

nice job!!

#

@grim cosmos ping so you know i answered

grim cosmos
#

Okay thanks

#

I completely forgot about this