IntValue Datasaving Issue

  1. What do you want to achieve? Keep it simple and clear!
    Save an IntValue when the current level ends or game binds to close.

  2. What is the issue? Include screenshots / videos if possible!
    Im not sure how I would save my IntValue because all info online just tells me about playerAdded and stuff.

  3. What solutions have you tried so far? Did you look for solutions on the Creator Hub?
    Looking online and dev fourms.

Code:

local ButtonDataStore = game:GetService("DataStoreService"):GetDataStore("ButtonData")

local clickDetector = script.Parent:FindFirstChild("Button").ClickDetector

local saveDataCount = 3

local function saveButtonData()
	local tableToSave = {
		script.Parent:FindFirstChild("DataValue").Value;
	}

	local success, errorMessage = pcall(ButtonDataStore.SetAsync, ButtonDataStore, ButtonDataStore, tableToSave)
	
	saveDataCount -= 1
	
	if success then
		warn("ButtonData has been saved!")
	else
		warn("ButtonData has not been saved!")
		warn(errorMessage)
		if saveDataCount > 0 then
			saveButtonData()
		else
			warn("No more attempts to save data!")
			saveDataCount = 5
		end
	end
end

workspace.Values.ActiveFloor.Changed:Connect(function()
	script.Parent:FindFirstChild("DataValue").Value = ButtonDataStore:GetAsync()
end)

clickDetector.MouseClick:Connect(function()
	script.Parent:FindFirstChild("DataValue").Value += 1
end)

game:BindToClose(function()
	task.spawn(saveButtonData)
end)

while true do
	task.wait()
	script.Parent.Screen.SurfaceGui.TextLabel.Text = "Total Clicks: " ..script.Parent:FindFirstChild("DataValue").Value
end

task.spawn(saveButtonData)

task.wait(workspace.Values.CurrentFloorTime.Value - 5)

Pictures:


To save and get data you need to use a key, in this case you can use the player’s userid.

I wanna store the buttons click data (script.Parent:FindFirstChild(“DataValue”).Value), not the players local click data.

Its a global button, so you click it 5 times and when you rejoin the server its still 5 and everyone has the same data.

If it saves all the player’s clicks you can use the same thing but set the key to something like “server”.

You’re passing ButtonDataStore twice, and you’re not specifying a key…

local success, errorMessage = pcall(ButtonDataStore.SetAsync, ButtonDataStore, ButtonDataStore, tableToSave)
local success, errorMessage = pcall(ButtonDataStore.SetAsync, ButtonDataStore, "SomeKey", tableToSave)

Replace “SomeKey” with player’s UserId. Also, your GetAsync() needs a key too.