Hey there people, I’m trying to make a datastore that would save a player’s current disease, but whenever a player leaves, this error comes up:
![]()
I tried googling the problem up, but there was nothing that could help me.
Here’s the source of the script:
local DS = game:GetService("DataStoreService")
local diseaseDS = DS:GetDataStore("DiseaseDS")
local function saveData(player)
local savedisease = player.Disease.Value
local success, err = pcall(function()
DS:SetAsync(player.UserId, savedisease)
end)
if success then
print("Disease "..tostring(player.Disease.Value).." has been saved")
else
print("Error whilst saving disease")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local Disease = Instance.new("StringValue")
Disease.Name = "Disease"
Disease.Parent = player
local data
local success, err = pcall(function()
data = DS:GetAsync(player.UserId)
end)
if success and data then
Disease.Value = data[1]
else
print("Player has no disease")
Disease.Value = "none"
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Disease saved")
else
print("Error whilst saving disease")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
saveData(player)
end)
if success then
print("Disease saved")
else
print("Error whilst saving disease")
end
end
end)```
cheers!