Hello
I wanted to make a DataStore script for my game (Script in ServerScriptService) and I tried using that and it doesn't work, can someone explain me why? The script including 49 lines.
Here is the script:
ocal 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 playerUserId = "Player_"..player.UserId
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Coins.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Coins.Value
local success, errormessage = pcall(function()
myDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
print("There was an error!")
warn(errormessage)
end
end)

2 comments
make sure to but an if data then because it will errrr
On the first line it says "ocal" instead of "local" and idk if that is a problem but just revise your script