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

Trying To create a leaderbord that says the top player in the game but it errors?

Asked by 6 years ago

here is the script

a = true
b = {}
c = {}
highest = 0
while a == true do
    wait (1)
    for _, player in pairs(game.Players:GetPlayers()) do
        if player.DataReady == true then

        PlayerName = player.Name
        local survived=player:LoadNumber("SurvivedTotal") or 0
        table.insert(b,player)
        table.insert(c,survived)

        d = 0
        for i,v in pairs(c)do
            d = d+1
            wait()
            if v > highest then
        highest = v
    end
        end
        script.parent.Text = (b[d]..": "..c[d])
        print (c[d])
        print (d)
        d = 0

script.parent.Text = (PlayerName..": "..survived)

        end
        d = 0
    end
    b = {}
    c = {}
    end

the output is:

(DIR).Script23: Attempt to concentrate field `?` (a userdata value)
0
pls wat is this Pejorem 164 — 6y

2 answers

Log in to vote
0
Answered by
IPsec 35
6 years ago
Edited 6 years ago

Hello,

I have went through your code and made some modifications! Please read the commented code for the rundown of what I've changed and why I've made these changes to your script.

If anything seems wrong, feel free to leave a comment, or change my code yourself!

-- use datastore
ds = game:GetService("DataStoreService"):GetGlobalDataStore()

a = true
-- notice replacing highest with highestPlayer
highestPlayer = nil

-- turn "b" and "c" into one dictionary table; dic[player] = # survived
dic = {}
-- decompose repeated / reusable code into functions
function updateHighest()
    -- theres no need for "d" anymore
    -- however, we don't know if the highest value is old
    -- need a value to keep track of current ingame highest
    local current_highest = -1
    local current_highestPlayer
    for player, survived in pairs(dic) do
        print(player, survived)
        -- also, no need to wait() here; small list of players to go through
        if survived > current_highest then
            current_highestPlayer = player
            current_highest = survived
        end
    end
    -- highest = highest of current in-game players
    highestPlayer = current_highestPlayer
end

function loadUser(player)
    local survived = ds:GetAsync(player.UserId .. "Survivals") or 0
    print(survived)

    dic[player] = survived
end

-- use an event that runs a function when something happens to load this users' survivals
-- i.e. game.Players.PlayerAdded => new user joins
game.Players.PlayerAdded:Connect(loadUser)

-- main runtime
while a == true do
    updateHighest()
    if highestPlayer then
        -- b[d] is now highestPlayer; c[d] is now dic[highestPlayer]
        script.Parent.Text = (highestPlayer.Name .. ": " .. dic[highestPlayer])
    else
        print("nobody is here!")
    end
    wait(1)
end

Please note I did not add a Saving function, because I did not notice one in the original script. I highly encourage you to try making a Saving function using DataStore.

0
i would but i kinda scripted around 200 lines of code using persistence so yeah... ForgotenR4 68 — 6y
Ad
Log in to vote
0
Answered by
Pejorem 164
6 years ago
Edited 6 years ago
  1. i == d
  2. b[d] is a player you cannot concatenate this
  3. c[d] is a number you can concatenate this
0
You might actually need d I couldn't really discern since your code is so badly organised visually. Pejorem 164 — 6y
0
im confused about what you said can you explain it a bit more in depth please? ForgotenR4 68 — 6y
0
You're trying to concatenate a player object. Pejorem 164 — 6y
0
Wait is this also using DataPersistance??!?!?! Pejorem 164 — 6y
View all comments (7 more)
0
Deprecated means DONT USE. Use DataStoreService not DataPersistance Pejorem 164 — 6y
0
welp ive programed around 200 lines of code using DataPersistance ForgotenR4 68 — 6y
1
im used to classic roblox scripting style ForgotenR4 68 — 6y
0
"classic" pls. bitch. Do you understand anything you're saying. Pejorem 164 — 6y
0
probably not if you want me to fix those words "old scripting methods" ForgotenR4 68 — 6y
0
If you mean procedural coding from the 60s/70s then you're p close ig Pejorem 164 — 6y
0
no not really from 60's 70's around 45 years later? ForgotenR4 68 — 6y

Answer this question