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

Same game,New error,Can you help me fix it? (If no RIP MY GAME DATABASE)

Here the error : 21:15:05.226 - ServerScriptService.SaveData:10: attempt to index number with 'Value'

And the script :

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.UserId
	local save1 = plr.leaderstats.Strength.Value
	local save2 = plr.leaderstats.Money.Value
	
	local GetSaved = DS:GetAsync(plrkey)
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
	else
		local NumberForSaving = {save1.Value, save2.Value}
		DS:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	DS:SetAsync("id_"..plr.UserId, {plr.leaderstats.Strength.Value, plr.leaderstats.Money.Value})
end)

2 answers

On line 05 and 06, you already retrieve the value. Numbers do not have the Value property.

Try removing the .Value properties from both of those lines.

Hello there.

Issue

You put a .Value before the strength instance. This would not work as you'd be trying to index a number, which is the instance's value.

Recommendation

Use :GetService() before indexing a service.

Fixed Code:

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")
game:GetService("Players").PlayerAdded:Connect(function(plr)
    wait()
    local plrkey = "id_"..plr.UserId
    local save1 = plr.leaderstats.Strength
    local save2 = plr.leaderstats.Money

    local GetSaved = DS:GetAsync(plrkey)
    if GetSaved then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
    else
        local NumberForSaving = {save1.Value, save2.Value}
        DS:GetAsync(plrkey, NumberForSaving)
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    DS:SetAsync("id_"..plr.UserId, {plr.leaderstats.Strength.Value, plr.leaderstats.Money.Value})
end)

Please accept my answer if it helped.

2 comments

0

GetService is an insurance to create a service if it's not there, otherwise to return the service. "Players" will always be there under every circumstance, so you don't need to use GetService for every call. Use GetService if you're unsure that the service can accessed when you need to be. Please don't suggest a recommendation without giving a clear description of how it helps. Cheers.

0

Sorry about that. For some reason I forgot to explain it in this answer.

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