You can write your topic however you want, but you need to answer these questions:
-
What do you want to achieve?
I want to successfully create a DataStore script -
What is the issue?
Well… Im trying to learn how to script but had a little problem:
While watching a tutorial about DataStore i wrote the entire script and tested it,but for some reason when i changed the value of the “Coins” and then stopped and started the game again it didn’t save! -
What solutions have you tried so far?
I have tried solving it by myself with the output, but it doesn’t tell me any errors and i also can’t find any!
Well,here is the script, i really don’t understand what im doing wrong here.
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("MyDataStore")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
local data
local success, errorMessage = pcall(function()
data = myDataStore:GetAsync(player.UserId)
end)
if success then
coins.Value = data
else
warn(errorMessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
local data = myDataStore:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)
if success then
print("Data Saved")
else
warn(errorMessage)
end
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetChildren()) do
player:Kick("Server Closed")
end
wait(2)
end)