I made this script from a youtube tutorial on DataStores and I'm relatively new to scripting. I reviewed my script multiple times and see no errors but it won't work. I have my API on too btw.
`` 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 Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = leaderstats
local playerUserId = "Player_"..player.UserId
-- Load Data
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(playerUserId)
end)
if success then
Cash.Value = data
--Set our data equal to current cash
end
end)
game.Players.PlayerRemoving:Connect(function(player) local playerUserId = "Player_"..player.UserId
local data = player.leaderstats.Cash.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) ``

1 comment
I tried this and it worked thank you!