HI out there i am learning to script and i made a datastore script that is supposed to store cash but its not saving and giving me an error message. I know i have been creating a lot of topics recently but that’s because i ak learning. Can someone help me with this anyways
THE CODE
local DataStoreService = game:GetService(“DataStoreService”)
local myDataStore = DataStoreService:GetDataStore(“DataStore”)
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 the current cash
end
end)
local data
game.Players.PlayerRemoving:Connect(function(player)
local playerUserId = “Player_”…player.UserId
local data = player.leaderstats.Cash.Value
local success, errormessagge = pcall (function()
myDataStore:SetASync(playerUserId,data)
end)
if success then
print ("Data Successfully Saved")
else
print("There was an error!")
warn ("errormessage")
end
end)``