Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

My datastore not storing the values?

Asked by
Prestory 1395 Moderation Voter
6 years ago
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

Log in to vote
2
Answered by 6 years ago

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)
0
THANKS ALOT! Prestory 1395 — 6y
Ad
Log in to vote
0
Answered by
LuaDLL 253 Moderation Voter
6 years ago
Edited 6 years ago

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)


Answer this question