So im doing this lil avatar customisation system, and i ran into a problem when trying to make it save
Heres the code:
local dataStoreService = game:GetService("DataStoreService")
local players = game:GetService("Players")
local mainStore = dataStoreService:GetDataStore('Main')
players.PlayerAdded:Connect(function(plr)
local avatar = Instance.new('Folder')
avatar.Parent = plr
avatar.Name = 'avatar'
local shirt = Instance.new('StringValue')
shirt.Parent = avatar
shirt.Name = 'shirt'
shirt.Value = "None"
local pants = Instance.new('StringValue')
pants.Parent = avatar
pants.Name = 'pants'
pants.Value = "None"
local hat = Instance.new('StringValue')
hat.Parent = avatar
hat.Name = 'hat'
hat.Value = "None"
--Loading
local success,data = pcall(function()
return mainStore:GetAsync(plr.UserId)
end)
if success then
if not data then
data = {"None","None","None"}
end
if typeof(data) ~= 'table' then
data = {data,0}
end
shirt.Value = data[1]
pants.Value = data[2]
hat.Value = data[3]
print(data[1])
print(data[2])
print(data[3])
else
--print("Data failure")
end
end)
--saving
players.PlayerRemoving:Connect(function(plr)
local shirt = plr:FindFirstChild("avatar"):FindFirstChild("shirt")
local pants = plr:FindFirstChild("avatar"):FindFirstChild("pants")
local hat = plr:FindFirstChild("avatar"):FindFirstChild("hat")
print(shirt.Value)
local success,err = pcall(function()
return mainStore:SetAsync(plr.UserId,{shirt.Value,pants.Value,hat.Value})
end)
if not success then
--print("ERR")
end
end)
The shirt, pants and hat values in Player are correct, printing after leaving also works. But when i try and rejoin, this happens:
Thanks for any help!