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

can you help me with my Datastore Problem?

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.

3 comments

0

For your second script, "PlayerLeaving" is not a thing. Switch it out with "PlayerRemoving"

0

Alright, thanks.

0

Get rid of the [1] on the line 21.

1 answer

✓ Accepted answer

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.

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