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

Cannot save value to data store?

I cannot save my level value to data store, everytime i load the data, it gives nil value. Here's the code:

local DataStoreService = game:GetService("DataStoreService");
local PD = DataStoreService:GetDataStore("PlayerData");

function SaveData(player)
	PD:SetAsync(player.usedId,player.PlayerData.Stats.Level.Value);
end

function LoadData(player)
	local success,err = pcall(function()
		PLD = PD:GetAsync(player.userId);
	end)
	if success then
		if PLD ~= nil then
			player.PlayerData.Stats.Level.Value = 1;
		else
			player.PlayerData.Stats.Level.Value = PLD;
		end
	else
		player.PlayerData.Stats.Level.Value = 1;
	end
end

game.Players.PlayerRemoving:Connect(function(plr)
    repeat wait() until plr.PlayerData;
    SaveData(plr);
end)

game.Players.PlayerAdded:Connect(function(plr)
    repeat wait() until plr.PlayerData;
    LoadData(plr);
end)

2 answers

You put this code:

if PLD ~= nil then --If PLD is not equal to nil
	player.PlayerStats.Level.Value = 1
else
	player.PlayerStats.Level.Value = PLD
end

Chances are your script got data, but you told it to set the data back to one instead of taking it. Had you not saved the data, it would have looked for data and found nothing, making the level nil.

basic misspell

you put

function SaveData(player)
    PD:SetAsync(player.usedId,player.PlayerData.Stats.Level.Value);
end

usedId it's meant to be UserId

function SaveData(player)
    PD:SetAsync(player.UserId,player.PlayerData.Stats.Level.Value);
end

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