I have asked this question before, but they weren't helpful and nobody is giving the answer.
My script errors in line 31, "ServerScriptService.Stats:31: attempt to index number with 'PlayerStage'".
Script:
local DataStoreService = game:GetService("DataStoreService")
local StageDataStore = DataStoreService:GetDataStore("StageDataStore")
local debounce = false
game:GetService("Players").PlayerAdded:Connect(function(player)
local players = game:GetService("Players"):GetPlayers()
for i, _ in pairs(players) do
if i > 1 then
player:Kick("There is more than 1 player in the server. Rejoin the game!")
return nil
end
end
local Stage = Instance.new("IntValue", player)
Stage.Name = "Stage"
player.RespawnLocation = game:GetService("Workspace"):WaitForChild("Start").Checkpoint["0"]
local playerUserId = "Player_"..player.UserId
-- Load Data
local data
local success, errormessage = pcall(function()
data = StageDataStore:GetAsync(playerUserId)
end)
if success then
-- Set data to stage
Stage.Value = data.PlayerStage
if data.PlayerStage then
game:GetService("ReplicatedStorage").StageChanged:FireClient(player, Stage.Value)
end
for i, v in pairs(data.PlayerObbies) do
game:GetService("ServerStorage").PassedStage:Fire(i)
end
end
end)
game:GetService("Players").PlayerRemoving:Connect(function(player)
local playerUserId = "Player_"..player.UserId
local PlayerStages = {}
for _, Obby in pairs(game:GetService("Workspace"):GetChildren()) do
if Obby.Name == "Obby1" or Obby.Name == "Obby2" then
for __, SpawnLocation in pairs(Obby.Checkpoint:GetChildren()) do
if SpawnLocation:IsA("SpawnLocation") then
table.insert(PlayerStages, tonumber(SpawnLocation.Name), Obby)
end
end
end
end
local data = {
PlayerStage = player.Stage.Value;
PlayerObbies = PlayerStages
}
local success, errormessage = pcall(function()
StageDataStore:SetAsync(playerUserId, data)
end)
if success then
print("Data successfully saved!")
else
warn("Error when saving data!")
warn(errormessage)
end
end)

3 comments
which line do i put this code in?
plus, what does save() do?
Put this BindToCLose at the end, save() is just a nil function taken as an eg. Just put ur saving code instead of where Save() is there.