local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local shirtId = character:WaitForChild("Shirt").ShirtTemplate
local pantsId = character:WaitForChild("Pants").PantsTemplate
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId, {shirtId, pantsId})
end)
if success then
shirtId = data[1]
pantsId = data[2]
else
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local shirtId = character:WaitForChild("Shirt").ShirtTemplate
local pantsId = character:WaitForChild("Pants").PantsTemplate
local success, errormessage = pcall(function()
local success, errormessage = pcall(function()
myDataStore:SetAsync(tostring(player.UserId), {shirtId, pantsId})
end)
if success then
print ("Player Data successfully saved")
else
print ("Error saving data")
warn(errormessage)
end
end)
end)
and it’s suppost to save clothes you bought in-game so after you leave/die you still got the clothes on you, the problem is that whenever i play-test the game the output says “Unable to cast value to Object”
In the GetAsync function, remove the second argument; GetAsync only takes one argument (or, extremely uncommonly, a second DataStoreGetOptions argument.)
You’re also using pcalls incorrectly, and your script will break when more than one player joins. Do this instead in the PlayerAdded function:
local success, data = pcall(function()
return myDataStore:GetAsync(player.UserId)
end)
This sets the data variable to be local in the PlayerAdded context, so it is a separate variable for each player. Also, the pcall in this case does not return an error message.
The same error appears multiple times actually, to solve that, put this at the top of the PlayerAdded function:
local shirtId
local pantsId
Also, in the PlayerRemoving function you shouldn’t be using multiple pcalls. Use only one, like this example in the wiki:
I’m pretty sure that will solve your problem but if it isn’t solved after you make these changes I can take another look.
What type of Instance is the ShirtTemplate and the PantsTemplate?
Edit: @Mob1leN0OB Assuming that the Instances are a Shirt and a Pants, a DataStore system like the one in this example should be enough to save their IDs:
local DataStoreService = game:GetService("DataStoreService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local function onPlayerAdded(player: Player)
local success, data = pcall(myDataStore.GetAsync, myDataStore, player.UserId)
if success then
print(data)
else
warn(data)
end
end
local function onPlayerRemoving(player)
if player.Character then
xpcall(myDataStore.SetAsync, warn, myDataStore, player.UserId, {
player.Character.Shirt.ShirtTemplate,
player.Character.Pants.PantsTemplate,
})
else
warn(string.format("Unable to save data for Player %i (%s) because Player.Character is nil", player.UserId, player.DisplayName))
end
end
Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)
if RunService:IsStudio() then
game:BindToClose(function()
task.wait(1)
end)
else
game:BindToClose(function()
local x, y = 0, 0
for _, player in Players:GetPlayers() do
x += 1
coroutine.wrap(function()
onPlayerRemoving(player)
y += 1
end)()
end
repeat task.wait() until y == x
end)
end
For testing purposes and to show that it works, I made it so that it prints the data retrieved for that player when they join the game
There are some important drawbacks I’d like to address with both my example and the method you’re currently using to manage the data, which are:
The character isn’t guaranteed to exist at the moment a player leaves the game, even when joining the game in certain situations
In the code you provided you’re attempting to set the IDs for the Shirt and Pants without first checking to see if the character has a Shirt and Pants, which unless you’re changing the character to one that includes them beforehand will cause errors
This is why I personally don’t recommend involving characters directly in your data saving system, since characters are quite unpredictable. Instead I suggest you save a table with boolean values that represent which clothes the player has bought in your game and then giving the player the clothes which are set to true once they join the game, which will prevent many future issues with your data system
it doesn’t work??? like i don’t know if you just can’t save clothes in-game or is the script not working because, it does everything its suppost to do but it just doesn’t apply the shirt and pants thingy
i suck at datastores and i actually tried something from my different game, when i buy a shirt a boolvalue inside the player value is set to true, and it will save the boolvalue, and if its true then apply the clothing, but it took too long and it still didnt work.