Teleportation script for my 2D platformer won't work. How do I fix this?

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!

image

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?

There is nothing in the output.

Assuming C is your debounce, you never set it to false after the first level is beaten.

That didn’t fix the problem. I tried doing it, and it teleported me to stage 38 (which doesn’t exist).

hmmm, well you need the C to equal false at some point so they can continue teleporting. Can you do me a favor and print out

tp.leaderstats.Level.Value

right after
tp.leaderstats.Level.Value = tp.leaderstats.Level.Value + 1

and tell me what you get?

2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.

Then, I am at course 13.

hmmm, I completely replicated your scripts into a studio session and it works great after I set C to equal false. I haven’t experienced any issues at all. I cant really replicate your problems, are there any more scripts in the game?

I changed things around. This is my new script. Everything look good?

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
				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
				print(tp.leaderstats.Level.Value)
				h.CFrame = lvls[tp.leaderstats.Level.Value].StartPart.CFrame
				c = false
			end
		end
	end)
end

Your previous script worked perfectly, just change the c to equal false in the coroutine. In this script you just sent, make sure c = true before the wait(0.1) so the player doesn’t start teleporting to every stage with one touch(Called a debounce I don’t know if you know what these are but here is more information on them :slight_smile: ). Then I would add a wait(.5) just to make sure the player had teleported.