Second Value not saving

Hello,

I am making an obby game, and I made a Datastore script that saves two values (more in the future), Stage and Skips. But when I tested the game, the stage value was saved perfectly fine, but the skips value was still at 0. Can anyone help?

local dataStoreService = game:GetService("DataStoreService")
local stageData = dataStoreService:GetDataStore("StageData")

local playerService = game:GetService("Players")

function saveTable(player)
	local tableToSave = {
		player.leaderstats.Stage.Value,
		player.leaderstats.otherValues.Skips.Value
	}

	local success, result = pcall(function()
		stageData:SetAsync(player.UserId, tableToSave) 
	end)
end

playerService.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = plr

	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats
	
	local otherValues = Instance.new("Folder")
	otherValues.Name = "otherValues"
	otherValues.Parent = leaderstats
	
	local skips = Instance.new("IntValue")
	skips.Name = "Skips"
	skips.Parent = otherValues

	local data
	local success, err = pcall(function()
		data = stageData:GetAsync(plr.UserId) 
	end)

	if success and data then 
		stage.Value = data[1]
		skips.Value = data[2]
	end
end)

playerService.PlayerRemoving:Connect(function(plr)
	local success, result = pcall(function()
		saveTable(plr)
	end)
end)
1 Like

maybe try to make a separate script for each value?

2 Likes

I fixed it by making an if statement

if data[1] then
	stage.Value = data[1]
end
		
if data[2] then
	skips.Value = data[2]
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.