Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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)

1 comment

0

pls wat is this

2 answers

✓ Accepted answer

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.

1 comment

0

i would but i kinda scripted around 200 lines of code using persistence so yeah...

  1. i == d
  2. b[d] is a player you cannot concatenate this
  3. c[d] is a number you can concatenate this

11 comments

0

You might actually need d I couldn't really discern since your code is so badly organised visually.

0

im confused about what you said can you explain it a bit more in depth please?

0

You're trying to concatenate a player object.

0

Wait is this also using DataPersistance??!?!?!

0

Deprecated means DONT USE. Use DataStoreService not DataPersistance

0

welp ive programed around 200 lines of code using DataPersistance

1

im used to classic roblox scripting style

0

"classic" pls. bitch. Do you understand anything you're saying.

0

probably not if you want me to fix those words "old scripting methods"

0

If you mean procedural coding from the 60s/70s then you're p close ig

0

no not really from 60's 70's around 45 years later?

This discussion is preserved as it was. New answers, comments, and votes are closed.