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

can you help me with my Datastore Problem?

Asked by 9 years ago

onPlayerAdded script

ServerStorage = game:GetService("ServerStorage")
local DataStore = game:GetService("DataStoreService"):GetDataStore("LevelStore")

game.Players.PlayerAdded:connect(function(player)
    local cameraScript = ServerStorage.CameraScript:Clone()
    player:WaitForChild("Backpack")
    cameraScript.Parent = player.Backpack
    cameraScript.Disabled = false
end)


game.Players.PlayerAdded:connect(function(player)
    local levels = Instance.new("IntValue", player)
    levels.Name =  "Levels"

    local key = "player-"..player.userId
    local savedValues = DataStore:GetAsync(key)

    if savedValues ~= nil then
        --Save format: (levels)
        levels.Value = savedValues[1]
    else
        local valuesToSave = (levels.Value)
        DataStore:SetAsync(key,valuesToSave)
    end
end)

I get an error with this script: 14:37:12.418 - Workspace.onPlayerAdded:21: attempt to index local 'savedValues' (a number value)

onPlayerLeaving script

local DataStore = game:GetService("DataStoreService"):GetDataStore("LevelStore")

game.Players.PlayerLeaving:connect(function(player)

    local key = "player-"..player.userId
    local valuesToSave = (player.levels.Value)
    DataStore:SetAsync(key,valuesToSave)    
end)

I got this error: 14:37:11.307 - PlayerLeaving is not a valid member of Players

Please help me fix these errors, thank you.

0
For your second script, "PlayerLeaving" is not a thing. Switch it out with "PlayerRemoving" alphawolvess 1784 — 9y
0
Alright, thanks. kingalpha1 15 — 9y
0
Get rid of the [1] on the line 21. EzraNehemiah_TF2 3552 — 9y

1 answer

Log in to vote
0
Answered by 9 years ago

In your first script, you are attempting to create a table like this: local valuesToSave = (levels.Value) However, wrapping a value in brackets doesn't put the value in a table. To make it a table, use: local valuesToSave = {levels.Value}. Since you just have the one value to save, you don't need a table - you could just save "levels.Value" and then load it up as a number. To do this, you just need to take out the [1] on line 21 (but keep line 23 the same, except maybe take out the parentheses) and it'll work as expected.

Ad

Answer this question