-
What I want to achieve is when a player clicks on a part it saves an accessory in datastore with table and when the player leaves and join it shows him the GUI of the item with options to equip it or unequip it
-
The problem is that StringValue is not saved when the player joins
Code one (The part when clicked)
function onClicked(plr)
if not plr:WaitForChild("Hats"):FindFirstChild(script.Parent.Name) then
plr.PlayerGui.OpenInventory.Frame["Blue Halo"].Visible = true
game.Players[plr.Name].PlayerGui.OpenInventory.Frame["BlueLocked"].Visible = false
local NewHalo = Instance.new("StringValue", plr:WaitForChild("Hats"))
NewHalo.Name = script.Parent.Name
NewHalo.Value = script.Parent.Name
game.Players[plr.Name].PlayerGui.OwnedOrNot.BlueNo.Visible = true
wait(3)
game.Players[plr.Name].PlayerGui.OwnedOrNot.BlueNo.Visible = false
else
game.Players[plr.Name].PlayerGui.OwnedOrNot.BlueYes.Visible = true
wait(3)
game.Players[plr.Name].PlayerGui.OwnedOrNot.BlueYes.Visible = false
end
end
script.Parent.ClickDetector.MouseClick:Connect(onClicked)
Code two(When the player joins it shows the GUI if he clicked the part)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("HatSaver") -- Change this with a different name.
game.Players.PlayerAdded:Connect(function(Player)
local Hats = Instance.new("Folder", Player)
Hats.Name = "Hats"
local PlayerKey = "Player_"..Player.UserId
local HatsData
local success, errormessage = pcall(function()
HatsData = DataStore:GetAsync(PlayerKey)
end)
if HatsData then
for _, v in pairs(HatsData) do
local HatValue = Instance.new("StringValue")
HatValue.Name = v
HatValue.Value = v
print(v.Name)
end
end
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Hats = Player:WaitForChild("Hats")
local PlayerHats = {}
local PlayerKey = "Player_"..Player.UserId
for _, v in pairs(Hats:GetChildren()) do
table.insert(PlayerHats, #PlayerHats, v.Name)
print("Saving "..v.Name)
end
DataStore:SetAsync(PlayerKey, PlayerHats)
end)