Hello,
I am making a 2D platformer called RUN: 2D. @profak and I have scripted everything, but there is one huge issue. Sometimes (not always), the game won’t teleport a player when they hit the “win part.” It won’t change their level either. Another issue is that when a player joins the game, it sometimes spawns them in the sky!

Here are the scripts:
Teleporter Script:
local lf = game.Workspace.Levels
local lvls = {}
local c = false
for i,level in pairs(lf:GetChildren()) do
local wp = level:WaitForChild("WinPart")
lvls[i] = level
wp.Touched:Connect(function(tou)
if c ~= true then
if game.Players:GetPlayerFromCharacter(tou.Parent) then
c = true
wait(0.1)
local tp = game.Players[tou.Parent.Name]
local h = tp.Character:WaitForChild("HumanoidRootPart")
tp.leaderstats.Level.Value = tp.leaderstats.Level.Value + 1
h.CFrame = lvls[tp.leaderstats.Level.Value].StartPart.CFrame
coroutine.resume(coroutine.create(function()
wait(0.1)
c = true
end))
end
end
end)
end
Leaderboard:
local DataStoreService = game:GetService("DataStoreService")
local myDataStore = DataStoreService:GetDataStore("platformer_version_2_6")
local loaded = false
game.Players.PlayerAdded:Connect(function(p)
local ls = Instance.new("Folder")
ls.Parent = p
ls.Name = "leaderstats"
local vcf = Instance.new("IntValue")
vcf.Parent = ls
vcf.Name = "Level"
vcf.Value = 1
local data
local success, errormessage = pcall(function()
data = myDataStore:GetAsync(p.UserId.."-level")
end)
if success then
vcf.Value = data or vcf.Value
loaded = true
else
print("ERROR WHILE GETTING DATA!")
warn(errormessage)
end
repeat wait()
until p.Character
local c = p.Character
if loaded ~= true then
repeat wait()
until loaded == true and c:FindFirstChild("HumanoidRootPart")
end
wait(2)
local lvls = {}
for _,level in pairs(workspace.Levels:GetChildren()) do
lvls[level:WaitForChild("Stage").Value] = level
end
local h = c:WaitForChild("HumanoidRootPart")
local ttt2 = vcf.Value or 0
h.CFrame = lvls[ttt2].StartPart.CFrame
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
myDataStore:SetAsync(player.UserId.."-level",player.leaderstats.Level.Value)
end)
if success then
print("OMG! IT WAS SAVED!")
else
print("NOOOO! THERE WAS AN ERROR!")
warn(errormessage)
end
end)
Anyone know why this isn’t working?