i made a script for datastore that for the first 2 days it worked but now it doesn’t work idk if i changed something by mistake i tried to write again to change the parent but nothing
this is the script:
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 data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
cash.Value = data
else
print("player has no data")
warn(errormessage)
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-cash", player.leaderstats.Cash.Value)
end)
if success then
print("successsfully saved data")
else
print("data save failed")
warn(errormessage)
end
end)
Try this make a new Variable called DefaultCash and if the loading fails make the players cash Default Cash
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("myDataStore")
local DefaultCash = 100
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 data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(player.UserId.."-cash")
end)
if success then
print(data)
cash.Value = DefaulthCash
print("Cash Has Been Loaded!")
else
print("player has no data")
warn(errormessage)
end
end)
@biicciioo Try this (make sure it’s a ServerScript inside of ServerScriptService):
local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("DataStore")
local defaultCash = 50 -- change amount to your liking
game.Players.PlayerAdded:Connect(function(plr)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Parent = leaderstats
local data
local success, message = pcall(function()
data = dataStore:GetAsync(plr.UserId.."-cash")
end
if success then
if data then
cash.Value = data
else
print(plr.Name.." is a new player")
cash.Value = defaultCash
end
else
warn(message)
end
end
game.Players.PlayerRemoving:Connect(function(plr)
local data = plr.leaderstats.Cash.Value
local success, message = pcall(function()
dataStore:SetAsync(plr.UserId.."-cash", data)
end
if success then
print("data for "..plr.Name.." saved")
else
warn(message)
end
end