Alright I shall explain.
The require() gets a module script that is responsible for the checkpoint script.
This is the code in the module,
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 leaderstats
if x:FindFirstChild("leaderstats") then
leaderstats = x:FindFirstChild("leaderstats")
else
leaderstats = Instance.new("Folder", x)
leaderstats.Name = "leaderstats"
end
local level = Instance.new("IntValue", leaderstats)
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")
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")
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 leaderstats
if plr:FindFirstChild("leaderstats") then
leaderstats = plr:FindFirstChild("leaderstats")
else
leaderstats = Instance.new("Folder", plr)
leaderstats.Name = "leaderstats"
end
local level = Instance.new("IntValue", leaderstats)
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")
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
the folder gets the parts in the checkpoint folder, the parts table is the table used to filter out the objects in the folder to make sure that they are not anything but a base part(part/mesh part). The loop adds all the parts to the table.
the print was just for me when I was testing to make sure everything worked.
Next, I got some variables including the data store and the data store that would be used to save the points.
I looped through the players at the beginning because the module takes some time to load and a player would have already been in the game.
Then I made it so that when a new player is added they have their points loaded and the level value is added.
I used the removing player to save the player’s points when they left the game.
For the final part, I looped through all the parts in the table and made it so that if you touch them, it will check if you are a level ahead of it and if you are not it will set your level to that value.
If you have any other questions on what I did in here please ask.
Here is a game I made quickly of it in use.
(2) Checkpoint Test - Roblox