as it is the script pretty much works however i want to rearrange it a bit because right now the player is spawned instantly at 0,0,0 position before the saved data can load it, preventing the script from moving the player to the checkpoint of the level he's curently on, i've tried all sorts of combinations, but it ends up causing even more of a delay. If any1 has an idea how to make it so that the player is teleported as soon as he enters the game i'd really appriciate it as i've been struggling with it all day
Thanks for your time
local data = game:GetService("DataStoreService") local dataStor = data:GetDataStore("MobbyProgress1") game.Players.PlayerAdded:connect(function(p) local stats = Instance.new("IntValue",p) stats.Name = "leaderstats" local stage = Instance.new("IntValue",stats) stage.Name = "Stage" stage.Value = 0 local Trophies = Instance.new("IntValue",stats) Trophies.Name = "Trophies" Trophies.Value = 0 local Trophy1 = Instance.new("BoolValue",p) Trophy1.Name = "Trophy1" Trophy1.Value = false local Trophy2 = Instance.new("BoolValue",p) Trophy2.Name = "Trophy2" Trophy2.Value = false local Trophy3 = Instance.new("BoolValue",p) Trophy3.Name = "Trophy3" Trophy3.Value = false key = 'MobbyProgress1_'..p.userId local saveData = dataStor:GetAsync(key) if saveData then stage.Value = saveData[1] Trophies.Value = saveData[2] Trophy1.Value = saveData[3] Trophy2.Value = saveData[4] Trophy3.Value = saveData[5] else dataStor:SetAsync(key, {0,0}) end p.CharacterAdded:Connect(function(Character) if Character ~= nil then local stats = p:WaitForChild("leaderstats") local stage = game.Workspace:WaitForChild(stats.Stage.Value) wait() Character.Torso.CFrame = stage.CFrame + Vector3.new(0,3,0) end end) game.Players.PlayerRemoving:connect(function(p) key = 'MobbyProgress1_'..p.userId local save = {p.leaderstats.Stage.Value, p.leaderstats.Trophies.Value,p.Trophy1.Value, p.Trophy2.Value,p.Trophy3.Value} dataStor:SetAsync(key, save) end) end)
Loading the player data will take time (it is a yield function) and as you connect the CharacterAdded event last the script will not run this code.
This will not solve your problem entirely as then we will not have any data loaded, there are multiple ways you could resolve this:-
Control when to spawn the player SH Question
Use the def information then respawn the player so that they load with the new stats ie after the data has loaded.
Spawn the player in a waiting area then respawn them when we have checked for stats.
There are probably more solutions, I do not know which would be best for your game so you will need to look at which approach you want to take.
Side notes:-
You currently are connecting the PlayerRemoving
event inside the PlayerAdded
event meaning you will connect this event for every player that joins the game, we only need one event to do the job this will fire multiple times as we connect a new event every time.
This should be moved outside of the PlayerAdded
scope.
I hope this helps.