Trouble With Finding Leaderstats Value For A Table

Hi, I am trying to save data to a datastore but the script to create a table won’t find the leaderstats value in the first place. I have made sure it is definitely a problem with this part of the script and not another part of the datastore process. Basically, what happens when I get it to print the values it prints the values as 0 instead of the value that the leaderstat is set to.

function create_table(player)
	local plr_stats = {}
	for i, v in pairs(player.leaderstats:GetChildren()) do
		plr_stats[v.Name] = v.Value 
	end
	return plr_stats
end

I am not sure why this is happening and I would like your advice on this, thank you.

1 Like

Maybe this will work.

local plr_stats = {}
function create_table(player)
	for i, v in ipairs(player.leaderstats:GetChildren()) do
		if not plr_stats[v.Name] then
			plr_stats[v.Name] = v.Value 
		end
	end
	return plr_stats -- or just get the plr_stats from the other functions
end

All I did was put the table outside the function and add a check to see weither there is something in the table already

Tip

please dont be like me and forget that ipairs exists (its faster than using just pairs. ipairs goes in order and pairs goes in a random order*)

1 Like

When will people learn that ipairs is faster than pairs.

2 Likes

I ALWAYS FORGET IM SO SORRY

(jokes aside but i always forget. I should edit that, thanks)

1 Like

Thank you, I’m not entirely sure why the script wasn’t working before but ill try and find out by testing it. Thank you again for the script edits and suggestions.

1 Like