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

My datastore not storing the values?

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("MeditationSaveSystem")
local ds2 = DataStore:GetDataStore("StrengthSaveSystem")
game.Players.PlayerAdded:Connect(function(player)
     local leaderstats = Instance.new("Model")
     leaderstats.Name = "leaderstats"
     leaderstats.Parent = player
     
     local Meditation = Instance.new("IntValue")
     Meditation.Name = "Meditation"
     Meditation.Value = ds:GetAsync(player.UserId) or 0
     Meditation.Parent = leaderstats
 
     local Strength = Instance.new("IntValue") 
     Strength.Name = "Strength" 
     Strength.Value = ds2:GetAsync(player.UserId) or 0
     Strength.Parent = leaderstats

game.Players.PlayerRemoving:Connect(function(player)
 ds:SetAsync(player.UserId, player.leaderstats.Meditation.Value)
 ds2:SetAsync(player.UserId,player.leaderstats.Strength.Value)
end)
end)

This code is meant to save the values when the player leaves but it does not save them may someone help me fix this thanks.

2 answers

✓ Accepted answer

Tried posting on your last question but it disappeared :p

Couple of problems; first, the PlayerRemoving event doesn't need to exist inside of the PlayerAdded event. Second, when a player exits the game, you set the value in the datastore PlayerCurrency at key player twice, rather than set the values of the datastores strength and meditation at key player.userId once; on top of that, you don't actually set the values in the datastores to valid values.

Try this out:

local currencystore = game:GetService("DataStoreService"):GetDataStore("PlayerCurrency");
local strengthstore = game.DataStoreService:GetDataStore("Strength");
local meditationstore = game.DataStoreService:GetDataStore("Meditation");

local create = function(name, classname, parent)
	local object = Instance.new(classname, parent);
	object.Name = name;
	return object;
end

game.Players.PlayerAdded:connect(function(player)
	local leaderstats = create("leaderstats", "Folder", player);
	local meditation = create("Meditation", "IntValue", leaderstats);
	local strength = create("Strength", "IntValue", leaderstats);
	strength.Value = (strengthstore:GetAsync(player.userId) or 0);
	meditation.Value = (meditationstore:GetAsync(player.userId) or 0);
end)

game.Players.PlayerRemoving:connect(function(player)
	strengthstore:SetAsync(player.userId, player.leaderstats.Strength.Value);
	meditationstore:SetAsync(player.userId, player.leaderstats.Meditation.Value);
end)

1 comment

0

THANKS ALOT!

Try this?

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("PlayerCurrency")

function create(name,classname,parent)
    local it = Instance.new(classname)
    it.Parent = parent
    it.Name = name
    return it
end
game.Players.PlayerAdded:Connect(function(plr)
    local ls = create("leaderstats","Folder",plr)
    local med = create("Meditation","IntValue",ls)
    local str = create("Strength","IntValue",ls)
	local plrData = dataStore:GetAsync(plr.userId)
	if plrData then
	med.Value = plrData[1]
	str.Value = plrData[2]
else
	local Save = {med.Value,str.Value}
	dataStore:SetAsync(plr.userId,Save)
end
end)
game.Players.PlayerRemoving:connect(function(plr)
    local SaveValues = {plr.leaderstats.Meditation.Value,plr.leaderstats.Strength.Value}
   local plrData = dataStore:GetAsync(plr.userId)
	if plrData then
		dataStore:SetAsync(plr.userId,SaveValues)
end
end)


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