Intvalue not saving

Hello, could someone please help me debug this issue?
Currently, there is a save script that is intended to save the player’s stage number (from leaderboard) into higheststage. However, the value is not saving AND not returning an error message, implying that there is an error with either highestvalue or stage.

Thank you for looking at this!

datastore = game:GetService("DataStoreService")
local higheststage = datastore:GetDataStore("HighestStage") --Container for a value
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	wait(5)
	userId = player.UserId --Get the player's id
	
	print(tostring(userId))
	
	stage = player.leaderstats.Stage --Leaderboard stage value
	
	local highestvalue = higheststage:GetAsync(userId) --Get value of higheststage for player
	
	print(tostring(highestvalue))
	print(tostring(stage.Value))
	
	
	stage.Value = highestvalue --Set stage to saved value
	
	print(tostring(stage.Value))
	
end)
	
players.PlayerRemoving:Connect(function()
	local setSuccess, errorMessage = pcall(function()
		higheststage:SetAsync(userId, stage.Value)
	end)

	if not setSuccess then
		errorMessage = "Data not saved!"
		warn(errorMessage)
	end
end)

Have you enabled Studio Access to API Services. This might be what is stopping the data from saving.

1 Like

Why you didnt follow data saving tutorails,go watch them.

1 Like

I used the roblox one and it broke

you didnt provide the player as a variable in the function (so userId doesnt work)
and you dont set stage somewhere in the function aswell

I set stage in another script for leaderboard
Also wdym that i didn’t set player (like do u mean i should’ve typed (function(player) bc i don’t use player anywhere in this)

Btw script and higheststage both print correctly

your saving and getting data is confusing me. idk why but i think you made a global variable with userId and stage, just do it like this:

local datastore = game:GetService("DataStoreService")
local higheststage = datastore:GetDataStore("HighestStage") --Container for a value
local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)	
	local success, data = pcall(function()
		return higheststage:GetAsync(tostring(player.UserId))
	end)

	if success then
		if not data then
			data = 1
		end
	print("yay success")
	end
	
	local highestvalue = data 
	
	local stage = Instance.new("IntValue")
	stage.Value = highestvalue
	stage.Name = "stage"
	stage.Parent = player
	stage.Changed:Connect(function()
		data = stage.Value
	end
end)
	
players.PlayerRemoving:Connect(function(player)
	local stage = player:FindFirstChild("stage")
	if stage then
		local setSuccess, errorMessage = pcall(function()
			higheststage:SetAsync(tostring(player.UserId), stage.Value)
		end)

		if not setSuccess then
			errorMessage = "Data not saved!"
			warn(errorMessage)
		end
	end
	
end)

I did this in another script called ‘leaderstatscript’ (sry i kinda rushed this)

local players = game:GetService("Players")

local function updateleaderboard(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local stage = Instance.new("IntValue")
	stage.Name = "Stage"
	stage.Parent = leaderstats
end

players.PlayerAdded:Connect(updateleaderboard)

Yes, I checked in settings and it’s turned on