How would i save workspace NumberValues

Im trying to save values that i have currently in workspace, i dont want to make leaderstats because it is kinda wierd, and its a solo game. this is my current code:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
	wait()
	local plrkey = "id_"..plr.userId
	local save1 = workspace.Apple.Value
	local save2 = workspace.Birch.Value
	local save3 = workspace.Hatchet.Value
	local save4 = workspace.Iron.Value
	local save5 = workspace.Leaf.Value
	local save6 = workspace.Matchet.Value
	local save7 = workspace["Oak Tree"].Value
	local save8 = workspace.Stick.Value
	local save9 = workspace.Stone.Value
	local save10 = workspace.Sulphur.Value
	local save11 = workspace.Wood.Value
	local save12 = workspace.ChestLoot.Diamond.Value
	local save13 = workspace.ChestLoot.Emerald.Value
	local save14 = workspace.ChestLoot.Glass.Value
	local save15 = workspace.ChestLoot.Gold.Value
	local save16 = workspace.ChestLoot.Pearl.Value
	local save17 = workspace.ChestLoot.Platnum.Value
	local save18 = workspace.ChestLoot.Ruby.Value
	local save19 = workspace.ChestLoot.Sapphire.Value
	local save20 = workspace.ChestLoot.Scrap.Value


	local GetSaved = ds:GetAsync(plrkey)
	if GetSaved then
		save1 = GetSaved[1]
		save2 = GetSaved[2]
		save3 = GetSaved[3]
		save4 = GetSaved[4]
		save5 = GetSaved[5]
		save6 = GetSaved[6]
		save7 = GetSaved[7]
		save8 = GetSaved[8]
		save9 = GetSaved[9]
		save10 = GetSaved[10]
		save11 = GetSaved[11]
		save12 = GetSaved[12]
		save13 = GetSaved[13]
		save14 = GetSaved[14]
		save15 = GetSaved[15]
		save16 = GetSaved[16]
		save17 = GetSaved[17]
		save18 = GetSaved[18]
		save19 = GetSaved[19]
		save20 = GetSaved[20]

	else
		local NumberForSaving = {save1.Value, save2.Value, save3.Value, save4.Value, save5.Value, save6.Value, save7.Value, save8.Value, save9.Value, save10.Value, save11.Value11, save12.Value, save13.Value, save14.Value, save15.Value, save16.Value, save17.Value, save18.Value, save19.Value, save20.Value, }
		ds:GetAsync(plrkey, NumberForSaving)
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync("id_"..plr.userId, 
		{
			workspace.Apple.Value,
			workspace.Birch.Value,
			workspace.Hatchet.Value,
			workspace.Iron.Value,
			workspace.Leaf.Value,
			workspace.Matchet.Value,
			workspace["Oak Tree"].Value,
			workspace.Stick.Value,
			workspace.Stone.Value,
			workspace.Sulphur.Value,
			workspace.Wood.Value,
			workspace.ChestLoot.Diamond.Value,
			workspace.ChestLoot.Emerald.Value,
			workspace.ChestLoot.Glass.Value,
			workspace.ChestLoot.Gold.Value,
			workspace.ChestLoot.Pearl.Value,
			workspace.ChestLoot.Platnum.Value,
			workspace.ChestLoot.Ruby.Value,
			workspace.ChestLoot.Sapphire.Value,
			workspace.ChestLoot.Scrap.Value,
		})
end)

when i leave and rejoin the values dont save, its on a local script, i have no idea on how to fix it

1 Like

This is extremely messy but I’ll keep your code style. Just set the .Value to what you saved.

Code:

local ds = game:GetService("DataStoreService"):GetDataStore("SaveData")

game.Players.PlayerAdded:Connect(function(plr)
	local save1 = workspace.Apple
	local save2 = workspace.Birch
	local save3 = workspace.Hatchet
	local save4 = workspace.Iron
	local save5 = workspace.Leaf
	local save6 = workspace.Matchet
	local save7 = workspace["Oak Tree"]
	local save8 = workspace.Stick
	local save9 = workspace.Stone
	local save10 = workspace.Sulphur
	local save11 = workspace.Wood
	local save12 = workspace.ChestLoot.Diamond
	local save13 = workspace.ChestLoot.Emerald
	local save14 = workspace.ChestLoot.Glass
	local save15 = workspace.ChestLoot.Gold
	local save16 = workspace.ChestLoot.Pearl
	local save17 = workspace.ChestLoot.Platnum
	local save18 = workspace.ChestLoot.Ruby
	local save19 = workspace.ChestLoot.Sapphire
	local save20 = workspace.ChestLoot.Scrap

	local GetSaved = ds:GetAsync(plr.UserId)
	
	if GetSaved then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
		save4.Value = GetSaved[4]
		save5.Value = GetSaved[5]
		save6.Value = GetSaved[6]
		save7.Value = GetSaved[7]
		save8.Value = GetSaved[8]
		save9.Value = GetSaved[9]
		save10.Value = GetSaved[10]
		save11.Value = GetSaved[11]
		save12.Value = GetSaved[12]
		save13.Value = GetSaved[13]
		save14.Value = GetSaved[14]
		save15.Value = GetSaved[15]
		save16.Value = GetSaved[16]
		save17.Value = GetSaved[17]
		save18.Value = GetSaved[18]
		save19.Value = GetSaved[19]
		save20.Value = GetSaved[20]
	end
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds:SetAsync(plr.UserId, {
		workspace.Apple.Value,
		workspace.Birch.Value,
		workspace.Hatchet.Value,
		workspace.Iron.Value,
		workspace.Leaf.Value,
		workspace.Matchet.Value,
		workspace["Oak Tree"].Value,
		workspace.Stick.Value,
		workspace.Stone.Value,
		workspace.Sulphur.Value,
		workspace.Wood.Value,
		workspace.ChestLoot.Diamond.Value,
		workspace.ChestLoot.Emerald.Value,
		workspace.ChestLoot.Glass.Value,
		workspace.ChestLoot.Gold.Value,
		workspace.ChestLoot.Pearl.Value,
		workspace.ChestLoot.Platnum.Value,
		workspace.ChestLoot.Ruby.Value,
		workspace.ChestLoot.Sapphire.Value,
		workspace.ChestLoot.Scrap.Value,
	})
end)
2 Likes

You will need to use a Server Script or a RemoteEvent that connects your Local Script to a Server Script.

1 Like

bump‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎ ‎

have you tried @Katrist’s answer? i think his answer might work

if it didn’t work, try using game:BindToClose() which fires when a server is shutting down or pressing the “stop” button when playtesting in roblox studio

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("SaveData")

local function loadData(Player: Player)
	local save1 = workspace.Apple
	local save2 = workspace.Birch
	local save3 = workspace.Hatchet
	local save4 = workspace.Iron
	local save5 = workspace.Leaf
	local save6 = workspace.Matchet
	local save7 = workspace:FindFirstChild("Oak Tree")
	local save8 = workspace.Stick
	local save9 = workspace.Stone
	local save10 = workspace.Sulphur
	local save11 = workspace.Wood
	local save12 = workspace.ChestLoot.Diamond
	local save13 = workspace.ChestLoot.Emerald
	local save14 = workspace.ChestLoot.Glass
	local save15 = workspace.ChestLoot.Gold
	local save16 = workspace.ChestLoot.Pearl
	local save17 = workspace.ChestLoot.Platnum
	local save18 = workspace.ChestLoot.Ruby
	local save19 = workspace.ChestLoot.Sapphire
	local save20 = workspace.ChestLoot.Scrap
	
	local Data = DataStore:GetAsync(tostring(Player.UserId))
	if Data ~= nil then
		save1.Value = GetSaved[1]
		save2.Value = GetSaved[2]
		save3.Value = GetSaved[3]
		save4.Value = GetSaved[4]
		save5.Value = GetSaved[5]
		save6.Value = GetSaved[6]
		save7.Value = GetSaved[7]
		save8.Value = GetSaved[8]
		save9.Value = GetSaved[9]
		save10.Value = GetSaved[10]
		save11.Value = GetSaved[11]
		save12.Value = GetSaved[12]
		save13.Value = GetSaved[13]
		save14.Value = GetSaved[14]
		save15.Value = GetSaved[15]
		save16.Value = GetSaved[16]
		save17.Value = GetSaved[17]
		save18.Value = GetSaved[18]
		save19.Value = GetSaved[19]
		save20.Value = GetSaved[20]
	end
end

local function saveData(Player: Player)
	DataStore:SetAsync(tostring(Player.UserId), {
		workspace.Apple.Value,
		workspace.Birch.Value,
		workspace.Hatchet.Value,
		workspace.Iron.Value,
		workspace.Leaf.Value,
		workspace.Matchet.Value,
		workspace:FindFirstChild("Oak Tree").Value,
		workspace.Stick.Value,
		workspace.Stone.Value,
		workspace.Sulphur.Value,
		workspace.Wood.Value,
		workspace.ChestLoot.Diamond.Value,
		workspace.ChestLoot.Emerald.Value,
		workspace.ChestLoot.Glass.Value,
		workspace.ChestLoot.Gold.Value,
		workspace.ChestLoot.Pearl.Value,
		workspace.ChestLoot.Platnum.Value,
		workspace.ChestLoot.Ruby.Value,
		workspace.ChestLoot.Sapphire.Value,
		workspace.ChestLoot.Scrap.Value
	})
end

local function onShutdown()
	if RunService:IsStudio() then
		task.wait(2)
	else
		local finished = Instance.new("BindableEvent")
		local allPlayers = Players:GetPlayers()
		local leftPlayers = #allPlayers

		for _, Player in ipairs(allPlayers) do
			task.spawn(function()
				saveData(Player)
				leftPlayers -= 1
				if leftPlayers == 0 then
					finished:Fire()
				end
			end)
		end

		finished.Event:Wait()
	end
end

for _, Player in ipairs(Players:GetPlayers()) do
	task.spawn(loadData, Player)
end

Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)
game:BindToClose(onShutdown)

credits to @GEILER123456 for the onShutdown function from his DataStore tutorial which you can read to learn more about data stores and if you want a very safe data store

Switch this to SetAsync

sadfdsgfhj

for some reason it doesnt work, i tried debugging using print() but it doesnt save any data

@Katrist’s script works, I tested it
Make sure you are changing the value on a normal script instead of a local script

can you screenshot the explorer window?

wdym changing the value, do you mean saving it or what

The script where you changed the NumberValues to make sure that it saves a different value, instead of the default ones
If you still don’t understand that I mean increasing the Apple, Birch, etc. amounts by one, two and so on

robloxapp-20231112-1046071.wmv (2.4 MB)
the values increase by clicking on it on a module script

i dont think i change it from a module script, maybe i could change the value adding onto a separate script


LocalPlayer indicates that you are requiring the module from a local script, which doesn’t get changed on the server (where data saving is based on), and the values stay the same there. I suggest using a RemoteEvent to handle adding the values on the server

bruh, all this time it was from a local script

there’s a huge difference between server and local

what server script basically means is that it can be accessed by the game server, and if something was changed by the server, it will show (replicate) to all clients (locals)

while locals, they are basically the players or clients (the client is the player’s device). if something was changed by a local script, it won’t replicate to other clients and the server

for example, there is player A and player B, and there’s the server

when a normal script (aka a server script) rotates a part in workspace, player A, player B, and the server itself can see the part rotated

when a local script (aka a client script) under player A 's PlayerGui (Players["Player A"].PlayerGui.LocalScript) rotates the same part, player A’s screen can see the part rotated, but for the player B’s screen (and the server’s “screen”) the part did not rotate

the solution for your problem is to use RemoteEvents. they can replicate (share) something from the client to the server

you can check the roblox docs on how to use the RemoteEvent