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

How to fix this datastore only displaying 1 value instead of 2?

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("Key22AcFLGGRRBBCC")

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder",plr)
	folder.Name = "leaderstats"
	local Subs = Instance.new("IntValue",folder)
	Subs.Name = "Subs"
	Subs.Value = ds:GetAsync(plr.UserId) or 0
	Subs.Changed:Connect(function()
	ds:SetAsync(plr.UserId, Subs.Value)
	local Views = Instance.new("IntValue",folder)	
	Views.Name = "Views"
	Views.Value = ds:GetAsync(plr.UserId) or 0
	Views.Changed:Connect(function()
	ds:SetAsync(plr.UserId, Views.Value)
	end)
	end)
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync(plr.UserId,plr.leaderstats.Subs.Value)
	ds:SetAsync(plr.UserId,plr.leaderstats.Views.Value)
end)

There is also nothing to see from views in the leaderstats folder?

1 answer

✓ Accepted answer

Your second stat was not showing up since it was nested within the .Changed event you had created. To save multiple datas to the same datastore, you can use tables which allow you to have multiple values in a single variable. I have also edited your code so that there is one function called statChange which saves the players data.

local dsService = game:GetService("DataStoreService")
local ds = dsService:GetDataStore("DataStored")

local function statChange(playerId, views, subs)
	print("Stats Changed")
	local playerData = {views.Value, subs.Value}
	ds:SetAsync(playerId, playerData)
end

game.Players.PlayerAdded:Connect(function(plr)
	
    local folder = Instance.new("Folder",plr)
    folder.Name = "leaderstats"


    local Subs = Instance.new("IntValue",folder)
    Subs.Name = "Subs"


	local Views = Instance.new("IntValue",folder)   
    Views.Name = "Views"
	
	local playerData = ds:GetAsync(plr.UserId) or nil
	print(playerData)
	
	if playerData then
		Views.Value = playerData[1]
		Subs.Value = playerData[2]
	end
	Subs.Changed:Connect(function()
		print("Works")
		statChange(plr.UserId, Views, Subs)
	end)	
	Views.Changed:Connect(function()
		statChange(plr.UserId, Views, Subs)
	end)

end)



game:BindToClose(function()
wait(5)	
end)

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