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

I making a script for my game that resets the leaderboard status but its not working?

Asked by
epoke466 100
3 years ago

Here is the script so far -

while true do
    wait(300)
    StatName = "Kills"
    Amount = 0
    wait(.1)
    Player = game.Players:GetPlayerFromCharacter(workspace[PlayerName])
    StatLeaderboard = Player.leaderstats[StatName]
        StatName = "Deaths"
    Amount = 0
    wait(.1)
    Player = game.Players:GetPlayerFromCharacter(workspace[PlayerName])
    StatLeaderboard = Player.leaderstats[StatName]

end

I want to reset the leader stats every 5 min for all players and the stats are Kills and Deaths

The script isn't working though and I dont know why, can you help me

Also if the script does look terrible, I only started a year ago

0
I dont know how to code this but you need a function to get all the children in players. Change there intvalue i think bcz7_Dev 12 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

I rewrote this to give you a better idea of how it should look. You can add some more if checks but if the rest of your code is working correctly you shouldn't have any errors.

Your code has several issues, but most importantly this 'Amount = 0' doesn't do anything. You need to create the values on entry, not at some interval.

--a simple function to iterate all players turning the specified objects value to 0, this is more flexible since it allows for any amount, any stat(inside leaderstats), and any group of players
function resetStat(players,stat,amount)
    for i,v in pairs(players) do
        v.leaderstats[stat].Value = amount
    end
end

--this adds the leaderstats folder and corresponding values when player joins the game
game.Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = Player
    local Kills = Instance.new("IntValue")
    Kills.Name = "Kills"
    Kills.Parent = Player.leaderstats
    local Deaths = Instance.new("IntValue")
    Deaths.Name = "Deaths"
    Deaths.Parent = Player.leaderstats
end)

--much cleaner, right? 
while wait(300) do
    local players = game:GetService('Players'):GetChildren()
    resetStat(players, 'Kills', 0)
    resetStat(players, 'Deaths', 0)
end
1
Line 4 will fail Ziffixture 6913 — 3y
0
Good eye, should've ran it through studio first. DinozCreates 1070 — 3y
0
Happens to the best of us ;) Ziffixture 6913 — 3y
0
It might also be better to assert the stat, to accommodate for existence issues! Ziffixture 6913 — 3y
View all comments (4 more)
0
thanks 4 the help epoke466 100 — 3y
0
wait I just tried the script and it didn't work, should of read Ziffixture comment first. I don't understand what is wrong with line 4? epoke466 100 — 3y
0
Whats the error? The problem he mentioned is fixed. DinozCreates 1070 — 3y
0
sorry i tried it before it was fixed epoke466 100 — 3y
Ad

Answer this question