When I make a script for datastore, It only works once, then it doesnt save anymore.
I have tried a couple of solutions like: Making seperate functions for saving and alot more, but I keep running into the same problem.
My code:
game.Players.PlayerRemoving:Connect(function(client)
SaveData(client)
end)
function SaveData(client)
local ClientId = client.UserId
local Data1 = script.HairId.Value
local Data2 = script.SkinColor.Value
local success, err = pcall(function()
HairId:SetAsync(ClientId,Data1)
end)
local success2, errorMessage = pcall(function()
ColorId:SetAsync(ClientId,Data2)
end)
print(success)
if success then
print("Success")
end
print(success2)
end
If it is saving once, it seems like it’s working and maybe the data is getting overwritten somewhere, like when the player joins?
I am assuming HairId and SkinColor are string values parented to the script itself?
Would it be possible to set them up as string values that are parented to the player’s leaderstats so that when another player joins, they don’t get overwritten?
Sorry I couldn’t be of more help, but I don’t see any issues with the code itself.
I would suggest to store both data too. Its not exactly related with ur problem, but could be useful
local DSS = game:GetService("DataStoreService")
local HairValues = DSS:GetDataStore("PlayerThings")
function SaveData(client)
local ClientId = client.UserId
local Data1 = script.HairId.Value
local Data2 = script.SkinColor.Value
local success, err = pcall(function()
HairValues:SetAsync(ClientId, {Data1, Data2})
end)
print(success)
if success then
print("Success")
end
end
game.Players.PlayerRemoving:Connect(function(client)
SaveData(client)
end)
function SaveData(client)
local ClientId = client.UserId
local Data1 = script.HairId.Value
local Data2 = script.SkinColor.Value
local success, err = pcall(function()
HairId:SetAsync(ClientId,Data1)
end)
local success2, errorMessage = pcall(function()
ColorId:SetAsync(ClientId,Data2)
end)
print(success)
if success then
print("Success")
end
print(success2)
Instead of having two datastores for the character’s appearance it would be better to save it into a table then have one.
local DataStoreService = game:GetService(DataStoreService
local AppearanceDataStore = DataStoreService:GetDataStore(“AppearanceDataStore”)
game.Players.PlayerRemoving:Connect(function(client)
SaveData(client)
end)
function SaveData(client)
local Data = {
HairId = script.HairId.Value,
SkinColor = script.SkinColor.Value}
local success, err = pcall(function()
AppearanceDataStore:SetAsync(ClientId, Data)
end)
print(success)
if success then
print("Success")
end
end
Sorry for the late reply I had to remind myself how to do this.
function loadData(player)
local data
local success, errormessage = pcall(function()
data = appearanceDataStore:GetAsync(player.UserId)
end)
local HairId = data.HairId
local SkinColor = data.SkinColor
end
game.Players.PlayerAdded:Connect(loadData)
Running into the same problem, it saves once and then doesnt save anymore.
So after the first time saving the table.
It doesnt return any errors, but also doesnt return any prints with the success value.
1.Use keys to store plr data
2.Never store .Value in a variable, for example.
Example
local a = script.Parent.Value
print(a) -- prints nothing.
local b = script.Parent
print(b.Value) -- prints the value.
Fixed Code
game.Players.PlayerRemoving:Connect(function(client)
SaveData(client)
end)
function SaveData(client)
local ClientId = "Key-"..client.UserId
local Data1 = script.HairId
local Data2 = script.SkinColor
local success, err = pcall(function()
HairId:SetAsync(ClientId,Data1.Value)
ColorId:SetAsync(ClientId,Data2.Value)
end)
print(success)
if success then
print("Success")
end
end