Hello, so im trying to make a clothing datastore that saves my clothes so i dont have to buy them in-game again. Here’s my script (its suppost to print “idk something wont work lol if it doesn’t save”)
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
print('idk something wont work lol')
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()
myDataStore = myDataStore:GetAsync(player.UserId)
end)
if success then
print ("Player Data successfully saved")
else
print ("Error saving data")
warn(errormessage)
end
end)
everytime i enter the game it prints out ‘idk something wont work lol’, can someone please help?
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)
end)
if success then
shirtId = data[1]
pantsId = data[2]
else
print('idk something wont work lol')
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()
myDataStore:SetAsync(player.UserId, {shirtId, pantsId})
end)
if success then
print ("Player Data successfully saved")
else
print ("Error saving data")
warn(errormessage)
end
end)
it’s probably because when the player has no saved data, data is being defined as nil (GetAsync returns nil if player has no data) and data is being indexed even thought it’s nil and not a table
so to fix this, you check if data exists (not nil) when setting the shirt template and pants template
i also noticed you were setting the variable instead of the ShirtTemplate property of Shirt (if you set a property of an instance as a variable, and you change that variable, the variable will change but not the property of the instance)
to also fix that, just set the property directly
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 shirt = character:WaitForChild("Shirt")
local pants = character:WaitForChild("Pants")
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success then
if data ~= nil then
shirt.ShirtTemplate = data[1]
pants.PantsTemplate = data[2]
end
else
print('idk something wont work lol')
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()
myDataStore:SetAsync(player.UserId, {shirtId, pantsId})
end)
if success then
print ("Player Data successfully saved")
else
print ("Error saving data")
warn(errormessage)
end
end)