So I have a shop that changes your outfit when you click on it. I am trying to make a datastore that will save the character’s last outfit they had on. My attempt at doing it wasn’t successful. What did I do wrong?
ServerScript:
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Outfit")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
local success, errormessage = pcall(function()
local information = {}
information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
local PlayerShirt = player.Character:FindFirstChild("Shirt").ShirtTemplate
local PlayerPants =player.Character:FindFirstChild("Pants").PantsTemplate
PlayerShirt = information.OutfitTop
PlayerPants = information.OutfitButtom
DataStore:SetAsync(player.UserId, information)
end)
if success then
print("Successfuly Saved!")
elseif errormessage then
warn(errormessage)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local success, errormessege = pcall(function()
return DataStore:GetAsync(playerUserId)
end)
if success then
print(player.Name.."Outfit Successfuly Stored!")
end
end)
Add HttpService = game:GetService("HttpService") at the top of your script
Change DataStore:SetAsync(player.UserId, information) to DataStore:SetAsync(player.UserId, HttpService:JSONEncode(information))
Explanation:
The SetAsync function requires you to input a string as a value AFAIK, and entering a table without first turning it into a string does not satisfy these parameters.
It doesn’t, it can take a non-mixed-key table, a string, integer or boolean. It’s JSON encoded when you go to call :SetAsync on the backend, and JSON decoded when you call :GetAsync
I reckon it’s saving correctly (with the exception of a few useless variables) but you didn’t load the data anywhere. Use the GetAsync method to get the data from the datastore.
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("datastore")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
local success, errormessage = pcall(function()
local information = {}
information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
DataStore:SetAsync(player.UserId, HttpService:JSONEncode(information))
end)
if success then
print("Successfuly Saved!")
elseif errormessage then
warn(errormessage)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local success, errormessege = pcall(function()
return DataStore:GetAsync(playerUserId)
end)
if success then
print(player.Name.."Outfit Successfuly Stored!")
end
end)
Taking a second look at this, it would seem you have your Removing and Added events swapped as well. You’re loading the data in whenever the player leaves and saving it whenever they first join. (In addition to not changing the player’s shirt/pants whenever the data is loaded in)
Also to further iterate on what @7z99 said previously, SetAsync is able to take tables as a data value.
So to save the player’s current stuff is it GetAsync or SetAsync? I’m pretty sure I did it right but it still didn’t work…
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("datastore")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
local success, errormessage = pcall(function()
local information = {}
information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
DataStore:GetAsync(player.UserId, HttpService:JSONEncode(information))
end)
if success then
print("Successfuly Saved!")
elseif errormessage then
warn(errormessage)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local success, errormessege = pcall(function()
return DataStore:SetAsync(playerUserId)
end)
if success then
print(player.Name.."Outfit Successfuly Stored!")
end
end)
local HttpService = game:GetService("HttpService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("datastore")
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(Character)
local success, errormessage = pcall(function()
local information = {}
information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
DataStore:GetAsync(player.UserId, HttpService:JSONEncode(information))
end)
if success then
print("Data Retrieved")
elseif errormessage then
warn(errormessage)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
local information = {}
information.OutfitTop = player.Character:WaitForChild("Shirt").ShirtTemplate
information.OutfitButtom = player.Character:WaitForChild("Pants").PantsTemplate
DataStore:SetAsync(player.UserId, HttpService:JSONDecode((information)))
end)
if success then
print("Data Succesfully Saved!")
elseif errormessage then
warn(errormessage) -- Error here
end
end)