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

Values wont save on datastore?

I'm trying to make a DataStore script but, my values won't save. I'm also watching tutrials on youtube on how to code DataStores to check if some of the parts of my code are wrong but still, no luck. Is there something wrong with the code?

Server Script:

local DataStore = game:GetService("DataStoreService")
local DS = DataStore:GetDataStore("CoinsAndGems")

game.Players.PlayerAdded:Connect(function(plr)
	
	local leaderboard = Instance.new("Folder", plr)
	leaderboard.Name = "leaderstats"
	
	local money = Instance.new("IntValue", leaderboard)
	money.Name = "Money"
	
	local key = "user"..plr.UserId
	local savedValues = DS:GetAsync(key)
	
	if savedValues then
		money.Value = savedValues
	else
		local saveValues = (money.Value)
		DS:SetAsync(key, saveValues)
	end
	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	
	local leaderboard = plr:WaitForChild("leaderstats")
	local money = leaderboard.Money
	local key = "user-"..plr.UserId
	
	DS:SetAsync(key, money.Value)
	
end)

2 answers

✓ Accepted answer
local DataStore = game:GetService("DataStoreService")
local DS = DataStore:GetDataStore("CoinsAndGems")

game.Players.PlayerAdded:Connect(function(plr)

    local leaderboard = Instance.new("Folder", plr)
    leaderboard.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderboard)
    money.Name = "Money"

    local key = "user-"..plr.UserId
    local savedValues = DS:GetAsync(key)

    if savedValues then
        money.Value = savedValues
    else
        local saveValues = (money.Value)
        DS:SetAsync(key, saveValues)
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)

    local leaderboard = plr:WaitForChild("leaderstats")
    local money = leaderboard.Money
    local key = "user-"..plr.UserId

    DS:SetAsync(key, money.Value)

end)

You misspelled the key on line 12

local DataStore = game:GetService("DataStoreService")
local DS = DataStore:GetDataStore("CoinsAndGems")

game.Players.PlayerAdded:Connect(function(plr)

    local leaderboard = Instance.new("Folder", plr)
    leaderboard.Name = "leaderstats"

    local money = Instance.new("IntValue", leaderboard)
    money.Name = "Money"

    local key = "user-"..plr.UserId
    local savedValues = DS:GetAsync(key)

    if savedValues then
        money.Value = savedValues
    else
        local saveValues = (money.Value)
        DS:SetAsync(key, saveValues)
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)

    local leaderboard = plr:WaitForChild("leaderstats")
    local money = leaderboard.Money
    local key = "user-"..plr.UserId

    DS:SetAsync(key, money.Value)

end)

You misspelled the key on line 12

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