Hi, so this is my first time working with Data stores. I have read documentation but here I have a problem that I can’t solve alone.
I made a script which should get 2 values from Data Stores (or initialize the variables if there is no value for the player) and save those 2 values when the player leaves.
Here is the script
local DataStoreService = game:GetService("DataStoreService")
local checkpointStore = DataStoreService:GetDataStore("PlayerCheckpoint")
local moneyStore = DataStoreService:GetDataStore("MoneyStore")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(player)
-- get checkpoint Data
local success, currentCheckpoint = pcall(function()
return checkpointStore:GetAsync(player)
end)
if success then
print("Current Checkpoint:", currentCheckpoint)
local checkPoint = Instance.new("IntValue", player)
checkPoint.Name = "CheckPoint"
checkPoint.Value = currentCheckpoint
else
print ("Checkpoint initialized")
local checkPoint = Instance.new("IntValue", player)
checkPoint.Name = "CheckPoint"
checkPoint.Value = 0
end
-- get money Data
local success, currentMoney = pcall(function()
return moneyStore:GetAsync(player)
end)
if success then
print("Current Money:", currentMoney)
local money = Instance.new("IntValue", player)
money.Name = "Money"
money.Value = currentMoney
else
print("Money initialized")
local money = Instance.new("IntValue", player)
money.Name = "Money"
money.Value = 0
end
end)
Players.PlayerRemoving:Connect(function(player)
-- set checkpoint Data
local playerCheckpoint = player.CheckPoint.Value
local success, err = pcall(function()
checkpointStore:SetAsync(player, playerCheckpoint)
end)
if success then
print("Checkpoint save Success")
else
print("Checkpoint save Error")
end
-- set money Data
local playerMoney = player.Money.Value
local success, err = pcall(function()
moneyStore:SetAsync(player, playerMoney)
end)
if success then
print("Money save Success")
else
print("Money save Error")
end
end)
The first part is working fine so far (it’s initializing my 2 variables).
But when I leave the game, nothing happens. It doesn’t print anything although I asked to print something whether it works or not.
I don’t know what’s wrong, I repeat it’s my first time working with Data Stores. If anyone can help it would be awesome.
Sorry for the long script, I thought it would be easier to understand with the entire code.
Sorry of I made English mistakes, it’s not my first language. Don’t hesitate to tell me.