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

Why does my DataStore save everything as 1? [NOT ANSWERED!]

--//variables
local DataStoreService = game:GetService("DataStoreService")
local LevelStore = DataStoreService:GetDataStore("Levelllssss")


--//player added
game.Players.PlayerAdded:Connect(function(player)
	
	--//creating level
	local LevelFolder = Instance.new("Folder",player)
	LevelFolder.Name = "LevelStore"
	local Level = Instance.new("IntValue",LevelFolder)
	Level.Name = "Level"
	Level.Value = LevelStore:GetAsync(player.UserId,Level.Value)or 1
	
	--//creating needed xp
	local NeededXP = Instance.new("IntValue",LevelFolder)
	NeededXP.Name = "NeededXP"
	NeededXP.Value = LevelStore:GetAsync(player.UserId,NeededXP.Value)or 1000
	
	--//creating current xp
	local XPP = Instance.new("IntValue",LevelFolder)
	XPP.Name = "XP"
	XPP.Value = LevelStore:GetAsync(player.UserId,XPP.Value)or 0
	
	--//main handling
	local success,level = pcall(function()
		return LevelStore:GetAsync(player.UserId,Level.Value,NeededXP.Value,XPP.Value)
	end)
	if success then
		LevelStore:SetAsync(player.UserId,Level.Value,NeededXP.Value,XPP.Value) else
		player:Kick("Data Error | Please rejoin!")
	end
end)


--//Save when player leaves
game.Players.PlayerRemoving:Connect(function(player)
	LevelStore:SetAsync(player.UserId,player.LevelStore.Level.Value,player.LevelStore.NeededXP.Value,player.LevelStore.XP.Value)
end)

So I started learning how to do datastores yesterday. Today I ran into a problem when trying to create my level system. When I first join the game, the values get set correctly. When I join the game for the second time to see if it all saved, it saves at one? Why is this? I'm new to datastores and kind of need help. I can't spot where it's all saving as 1.

9 comments

0

It's actually much neater. It's just on here that it kind of messes up with neatness

0

Is XPP saving at one as well?

0

Yeah it's saving on player leaving, it's cut off though and it's saving everywhere else

0

Oh, yes XPP is saving as one as well

0

"Levelllssss"

0

Please answer my question instead of commenting on the name of the datastore ;-; I had to keep changing it because of it saving weirdly...

0

oh srry

0

GetAsync only takes one parameter, the key. And as I see from your SetAsync below, your key is the player's userid. Try getting rid of the second parameters in GetAsyncs and check the specific part of the table you want.

0

And SetAsync only takes two parameters, so maybe you should turn everything after the userid into a table or dictionary

1 answer

✓ Accepted answer

If you want to save multiple values to the same datastore, you will need to wrap them in a table. I have modified your code to show how this could be done:

--//variables
local DataStoreService = game:GetService("DataStoreService")
local LevelStore = DataStoreService:GetDataStore("Levelllssss")


--//player added
game.Players.PlayerAdded:Connect(function(player)
	-- Get the levelstore data into a variable (levelInfo)
	local success, levelInfo = pcall(function()
		return LevelStore:GetAsync(player.UserId)
	end)
	if success then
		-- If there was no data, then setup levelInfo with default values
		if not levelInfo then
			levelInfo = { Level = 1, NeededXP = 1000, XPP = 0 }
		end
	else
		-- Error retrieving data from datastore
		player:Kick("Data Error | Please rejoin!")
	end
		
	--//creating level folder
	-- Avoid using the parent option of Instance.new, as it can cause lag. Prefer to setup your object and then parent it manually
	local LevelFolder = Instance.new("Folder")
	LevelFolder.Name = "LevelStore"
	LevelFolder.Parent = player
	
	--//Creating level
	local Level = Instance.new("IntValue", LevelFolder)
	Level.Name = "Level"
	Level.Value = levelInfo.Level
	
	--//creating needed xp
	local NeededXP = Instance.new("IntValue", LevelFolder)
	NeededXP.Name = "NeededXP"
	NeededXP.Value = levelInfo.NeededXP
	
	--//creating current xp
	local XPP = Instance.new("IntValue", LevelFolder)
	XPP.Name = "XP"
	XPP.Value = levelInfo.XPP
	
	-- No need to save data here. If the data was nil then you're just saving default values, which is not needed,
	-- and if it wasn't nil, you're just saving the same info the datastore already contained back into it
end)


--//Save when player leaves
game.Players.PlayerRemoving:Connect(function(player)
	local levelInfo = { Level = player.LevelStore.Level.Value, NeededXP = player.LevelStore.NeededXP.Value, XPP = player.LevelStore.XP.Value }
	local success = pcall(function()
    	LevelStore:SetAsync(player.UserId, levelInfo)
	end)
	-- Need to deal with data failing to save
end)

Note that if you are testing this in play solo in studio, the game will exit before anything gets saved. If you create a local server in studio or try it in a published game then everything will work correctly. Also, I recommend following the tutorial on this page for your data saving code. This will give you much more robust error handling.

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