Hello!
(PLEASE READ THE COMMENTS TO SEE WHERE I AM AT)
While developping my game, I wanted to learn how to use DataStores, everything works fine, exept one thing. One very annoying thing:
The annoying thing
data doesn’t always save
Yep, Data doesn’t always save. Which is actually annoying. Worse even is when it works then later it won’t and rarily save.
(Please keep in mind:
I started with one DataValue just to get the basics and I’m gonna add more.
I’ll share the scripts with the only data to see if there’s any small but consequential.
it doesn’t say any errors it would normally say)
So I’ll share my scripts here:
Base Data script
Script Type: Normal Script
Parent: ServerScriptService
Notes: I will add more Data, I just started with one to get the basics.
Code:
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local Name = Instance.new("StringValue")
Name.Name = "NickName"
Name.Parent = leaderstats
local PlayerUserid = "Player_"..player.UserId
local Data
local success, errormessage = pcall(function()
Data = DataStore1:GetAsync(PlayerUserid)
end)
if success then
if Data ~= nil then
Name.Value = Data
else
Name.Value = player.Name
end
else
print(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local PlayerUserid = "Player_"..player.UserId
local Data = player.leaderstats.NickName.Value
local success, errormessage = pcall(function()
DataStore1:SetAsync(PlayerUserid, Data)
end)
if success then
print("Data was saved")
else
print("There was an error")
warn(errormessage)
end
end)
Second Script
Script Type: local
Parent(s): TextBox, Frame, Frame, Starter/PlayerGui
Notes: This is based on Nicknames, so this is why I used a TextBox. But please know that the important part is wether I’m doing good.
Code:
local TextBox = script.Parent
local Event = game.ReplicatedStorage.DumbInfo.NickName
--It's a folder, and a RemoteEvent
TextBox.InputEnded:Connect(function()
local Text = tostring(TextBox.Text)
Event:FireServer(Text)
end)
Third Script
Script Type: Normal
Parent: ServerScriptService
Notes: This is for receiving the event and changing the Value of the Data mentionned.
Code:
local DataStoreService = game:GetService("DataStoreService")
local DataStore1 = DataStoreService:GetDataStore("DataStore1")
local Event = game.ReplicatedStorage.DumbInfo.NickName -- Put RemoteEvent here
Event.OnServerEvent:Connect(function(Player, Text)
local NickName = Player.leaderstats.NickName
NickName.Value = Text
local success, newName = pcall(function()
return DataStore1:UpdateAsync("Player_1234", Text)
end)
if success then
print("New NickName:", NickName.Value)
end
print("New NickName: "..NickName.Value)
end)
Fourth Script
Script Type: Normal
Parent: ServerScriptService
Notes: Basically, it’s used to check whether you change the value already and save it in the TextBox.
Code:
game.Players.PlayerAdded:Connect(function(player)
local GUI = player.PlayerGui:WaitForChild("PlayersCard").Frame.NNFrame
local TextLabel = GUI.Default
local TextBox = GUI.ChangeNickName
print("PLAYER JOINED")
print(player.Name.." is crazy") -- I used it to check if we have the correct player, I do dumb stuff in my Outputs.
local leaderstats = player:WaitForChild("leaderstats")
local NickName = leaderstats.NickName
print(NickName.Value)
if NickName.Value == "" or nil then
print("There is no text")
TextLabel.Visible = false
else
print("There is text")
TextLabel.Visible = true
TextLabel.Text = NickName.Value
TextBox.Text = NickName.Value
end
end)
So basically…
Sometimes Data is saved (rarily), sometimes data is wiped (often) and sometimes data isn’t saved at all (often). Data being wiped or not saved are the most often things to occur.
This was a bit long but thanks to those who helped. I’ve been trying to fix this for hours with no good results.
Thanks for reading.