Hello! I’m working on an obby game and I’m trying to figure out what the problem is with the MainModule of the checkpoint system. When you spawn into the game, you spawn at the very start of the obby.
If you reset character/die, then the script will work as intended and send you to the correct checkpoint.
Here’s the script:
local module = {}
--Adding and loading data
-- variables
local folder = workspace:WaitForChild("CheckpointsFolder")
local prts = {}
for _,x in pairs(folder:GetChildren())do
if x:IsA("BasePart") then
table.insert(prts, x)
end
end
print("load")
local players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local myData = dataStore:GetDataStore("^%%58765^%$5")
for _,x in pairs(players:GetChildren())do
local learderstats = Instance.new("Folder", x)
learderstats.Name = "leaderstats"
local level = Instance.new("IntValue", learderstats)
level.Name = "Level"
local d
local s,e = pcall(function()
d = myData:GetAsync(x.UserId)
end)
if s then
level.Value = d
else
warn(e)
end
local char = x.Character or x.CharacterAdded:Wait()
print("Character added")
print("lala")
for _,c in pairs(prts)do
if c.Name == tostring(level.Value) then
print('a')
wait(.1)
local leftFoot = char:FindFirstChild("HumanoidRootPart")
print(leftFoot)
leftFoot.CFrame = c.CFrame * CFrame.new(0,10,0)
end
end
x.CharacterAdded:Connect(function(char1)
print("Character added")
print("lala")
for _,c in pairs(prts)do
if c.Name == tostring(level.Value) then
print('a')
wait(.1)
local leftFoot = char1:FindFirstChild("HumanoidRootPart")
print(leftFoot)
leftFoot.CFrame = c.CFrame * CFrame.new(0,10,0)
end
end
end)
end
players.PlayerAdded:Connect(function(plr)
local learderstats = Instance.new("Folder", plr)
learderstats.Name = "leaderstats"
local level = Instance.new("IntValue", learderstats)
level.Name = "Level"
local d
local s,e = pcall(function()
d = myData:GetAsync(plr.UserId)
end)
if s then
level.Value = d
else
warn(e)
end
plr.CharacterAdded:Connect(function(char)
print("Character added")
print("lala")
for _,c in pairs(prts)do
if c.Name == tostring(level.Value) then
print('a')
wait(.1)
local leftFoot = char:FindFirstChild("HumanoidRootPart")
print(leftFoot)
leftFoot.CFrame = c.CFrame * CFrame.new(0,10,0)
end
end
end)
end)
players.PlayerRemoving:Connect(function(plr)
local l = plr:WaitForChild("leaderstats")
local lvl = l:WaitForChild("Level")
local lvlVal = lvl.Value
local plrID = plr.UserId
local s,e = pcall(function()
myData:SetAsync(plrID, lvlVal)
end)
if s then
warn(plr.Name .. "'s level data was saved to " .. tostring(lvlVal))
else
warn(e)
end
end)
for c = 1,#prts do
prts[c].Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChildWhichIsA("Humanoid") then
local char = Hit.Parent
local plr = players:GetPlayerFromCharacter(char)
local lead = plr:WaitForChild("leaderstats")
local lvl = lead:WaitForChild("Level")
local lvlVal = lvl.Value
local partVal = tonumber(prts[c].Name)
if partVal > lvlVal then
lvl.Value = partVal
end
end
end)
end
return module
I think he wants it to save your last checkpoint when you leave the game. Then, when you return, you spawn at the last checkpoint, not at the beginning.
The reason the wait() solution doesn’t work is because you can’t really rely on wait() for loading things, as loading things doesn’t have a consistent timer and wait() itself isn’t supposed to be used for loading. Instead, you should use :WaitForChild(char) so it gets the character as soon as it loads.
I wrote this on mobile so there might be some typos.
local module = {}
--Adding and loading data
-- variables
local folder = workspace:WaitForChild("CheckpointsFolder")
local prts = {}
for _,x in pairs(folder:GetChildren())do
if x:IsA("BasePart") then
table.insert(prts, x)
end
end
print("load")
local Players = game:GetService("Players")
local dataStore = game:GetService("DataStoreService")
local myData = dataStore:GetDataStore("^%%58765^%$5")
function newPlayer(Player)
local leaderstats = Instance.new("Folder",Player)
leaderstats.Name = "leaderstats"
local level = Instance.new("IntValue",leaderstats)
level.Name = "Level"
local function newCharacter(Character)
print("Character Detected")
local PlayerLevel = tostring(level.Value)
for _, c in ipairs(prts) do
if c.Name == PlayerLevel then
wait(.1)
local HRP = Character:WaitForChild("HumanoidRootPart")
HRP.CFrame = c.CFrame * CFrame.new(0,10,0)
end
end
end
local d
local s, e = pcall(function()
d = myData:GetAsync(Player.UserId)
end)
if s then
level.Value = d
else
warn(e)
end
local Character = Player.Character or Player.CharacterAdded:Wait()
if Character then
newCharacter(Character)
end
Player.CharacterAdded:Connect(newCharacter)
end
for _, Player in pairs(Players:GetPlayers()) do
coroutine.wrap(function()
newPlayer(Player)
end)()
end
Players.PlayerAdded:Connect(newPlayer)
Players.PlayerRemoving:Connect(function(plr)
local l = plr:WaitForChild("leaderstats")
local lvl = l:WaitForChild("Level")
local lvlVal = lvl.Value
local plrID = plr.UserId
local s,e = pcall(function()
myData:SetAsync(plrID, lvlVal)
end)
if s then
warn(plr.Name .. "'s level data was saved to " .. tostring(lvlVal))
else
warn(e)
end
end)
for c = 1,#prts do
prts[c].Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChildWhichIsA("Humanoid") then
local char = Hit.Parent
local plr = Players:GetPlayerFromCharacter(char)
local lead = plr:WaitForChild("leaderstats")
local lvl = lead:WaitForChild("Level")
local lvlVal = lvl.Value
local partVal = tonumber(prts[c].Name)
if partVal > lvlVal then
lvl.Value = partVal
end
end
end)
end
return module