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

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

Asked by 4 years ago

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

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
4 years ago

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.

Ad
Log in to vote
0
Answered by 4 years ago

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.

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. Fifkee 2017 — 4y
0
Sorry about that. For some reason I forgot to explain it in this answer. youtubemasterWOW 2741 — 4y

Answer this question