This unwanted error appears on in the output: attempt to index number with number, why?

l

ocal Players = game:GetService("Players")
local DSS = game:GetService("DataStoreService")
local MyDataStore = DSS:GetDataStore("leaderstats")


Players.PlayerAdded:Connect(function(plr)
	local ls = Instance.new("Folder")
	ls.Name = "leaderstats"
	ls.Parent = plr
	
	
	for i,v in pairs(game:GetService("ReplicatedStorage"):WaitForChild("StarterLeaderstats"):getChildren()) do
		
		local IntValue = v:Clone()
		IntValue.Value =  MyDataStore:GetAsync(plr.UserId[i]) or 0 -- this line
		IntValue.Parent = ls
	end
end)


Players.PlayerRemoving:Connect(function(plr)
	local ls = {}
	for i,v in pairs(plr.leaderstats:GetChildren()) do
		table.insert(ls, v.Value)
	end
	
	MyDataStore:SetAsync(plr.UserId, ls)
end)

the comment that says this line, it is the line that is giving me the error,
it says:

attemp to index number with number.

HUH?! i did NOT do: plr.UserId[i] but i did:


MyDataStore:GetAsync(plr.Userd[1])

i it included in GetAsync

Why are you trying to index UserId with anything in the first place? The error is literal, it’s telling you that you’re trying to index a number property or child in a number which is illegal. UserId should just be fed standalone, don’t add any brackets.

2 Likes

but i inculded it in GetAsync!

I don’t know what you mean by that or what you’re trying to say here… it doesn’t matter. Numbers can’t be indexed by anything so don’t do it. Please read my first post carefully and try applying the feedback that I offered there.

2 Likes

you should read this!!!:

I already know how DataStores work. I pointed out what’s wrong with your code and I don’t want to repeat what I’ve said again. All I can tell you is to read my feedback carefully, read over your own code carefully and try applying the suggestion in your own code or debug your code carefully. I’m not going to spoonfeed you the answer here. You can’t index a number by a number. Furthermore, I don’t really get what you’re trying to convey in your responses.

3 Likes

is this what you’re trying to do?
MyDataStore:GetAsync(plr.UserId)[i]

2 Likes

yes!
but the value did not save why?

@cyswip

are there any errors you got in the output?

1 Like

no, there is no error, even if i used pcall function no error.

MyDataStore:GetAsync(plr.UserId[i])
MyDataStore:SetAsync(plr.UserId, ls)

Your issue is quite clear, you’re attempting to load data from a different key to the one you’re storing data to.

Remove [i] from the SetAsync() call.

1 Like

10/10 code, minor spelling typo

Jokes aside, did you actually forget to add an L behind ocal ? Wait you already got an L.

1 Like