Basically, when I entered my game after a while, my leaderstats and character to stage script stopped working. I attempted to fix the problem on my own, but I am unable to identify the root cause of the problem. It’s probably extremely obvious, and if I knew how, I’d like to give a thorough explanation, but I’m not sure what the reason is.
Logs:
local Players = game:GetService("Players")
local DS = game:GetService("DataStoreService"):GetDataStore("Stage Save")
local function OnCharacterAdded(char)
game:GetService("RunService").Stepped:Wait()
local plr = game.Players:GetPlayerFromCharacter(char)
char:WaitForChild("HumanoidRootPart").CFrame = workspace.Checkpoints[tostring(plr.TeleportedStage.Value)].CFrame + Vector3.new(0,3.25,0)
end
function OnPlayerAdded(plr)
plr.CharacterAdded:Connect(OnCharacterAdded())
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = plr
local stage = Instance.new("IntValue")
stage.Name = "Stage"
stage.Parent = stats
local TeleStage = Instance.new("IntValue")
TeleStage.Name = "TeleportedStage"
TeleStage.Parent = plr
local key = "id_" .. plr.userId
local data = DS:GetAsync(key)
if data then
stage.Value = data
TeleStage.Value = stage.Value
else
DS:GetAsync(key, stage)
end
end
Players.PlayerAdded:Connect(OnPlayerAdded)
function OnPlayerRemoved(plr)
local key = "id_" .. plr.userId
local data = plr.leaderstats.Stage.Value
DS:SetAsync(key, data)
end
Players.PlayerRemoving:Connect(OnPlayerRemoved)