-
What do you want to achieve? Keep it simple and clear!
Save an IntValue when the current level ends or game binds to close. -
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. -
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: