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.
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.