Not teleporting me to the accurate stage?

Read carefully so you’ll understand;

I’m having an issue where a Int-value named SpawnPoint which acts as a placeholder for previous stage values so you’ll respawn there instead of at your farthest stage (useful for helping friends on previous stages or replaying stages). Anyways, I have a datastore script where when a player resets/dies it’ll instantly teleport you to the SpawnPoint which doesn’t happen here for whatever reason which is the main issue. Instead, it’ll spawn you at your farthest stage which is another bool-value called Stage. Though, the strange thing is in the datastore script where it detects It clearly should teleport me to the SpawnPoint value as shown below:

The entire function in the datastore script (at the bottom of the function is where the issue is located, you’ll notice it if you’ve read the first paragraph of my post):


game.Players.PlayerAdded:Connect(function(player)
	local key = "player-" .. player.UserId

	local GetSave = DataStore:GetAsync(key) -- check for existing data 

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local hidden = Instance.new("Folder", player)
	hidden.Name = "hidden"

	local checkpoint = Instance.new("IntValue", leaderstats)
	checkpoint.Name = "Stage"

	local spawnpoint = Instance.new("IntValue", hidden)
	spawnpoint.Name = "SpawnPoint"

	if GetSave then
		checkpoint.Value = GetSave
		print("Data Loaded For " .. player.Name)
	else
		checkpoint.Value = 1
		print("New Data Created For " .. player.Name)
	end

	player.hidden.SpawnPoint.Value = player.leaderstats.Stage.Value

	local character = player.Character or player.CharacterAdded:Wait()
	local respawn2 = cpFolder:WaitForChild(player.leaderstats.Stage.Value)
	character:WaitForChild("HumanoidRootPart").CFrame = respawn2.CFrame + Vector3.new(0,3,0)

	player.CharacterAdded:Connect(function(character)

		repeat wait() until workspace:FindFirstChild(character.Name)
		local player = game.Players:GetPlayerFromCharacter(character)
		local respawn = cpFolder[player.hidden.SpawnPoint.Value]

		character.HumanoidRootPart.CFrame = respawn.CFrame + Vector3.new(0,3,0)
	end)

end)

Here are a few other scripts that may be effecting the teleportation for this script. One to believed to be doing this is the Checkpoint Handler where the touching of the stages are handled and executed. The bottom of this pairs loop is where the SpawnPoint is located and executed. This simply checks if the SpawnPoint value is equal to the Current value. The current value is the value of where you’re currently located along the stages whether your at stage 1, 24, 213, etc. I made it equal to this so if you were to shuffle through levels and the SpawnPoint would change whenever you press < or > your SpawnPoint would change to the Current value. (If you’ve ever played a difficulty chart obby, you’ll know what I mean but you don’t really need to understand that to figure out the issue I believe)

The Checkpoint Handler script:

local checkpoints = workspace:WaitForChild("Checkpoints")

for i,v in pairs(checkpoints:GetChildren()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				local player = game.Players:GetPlayerFromCharacter(hit.Parent)
				if tonumber(v.Name) == player.leaderstats.Stage.Value + 1 then --Checks if the new checkpoint is only increasing by 1.
					player.leaderstats.Stage.Value = tonumber(v.Name)
					player.hidden.SpawnPoint.Value = tonumber(v.Name)
					if player.hidden.SpawnPoint.Value == player.PlayerGui.Level.Current.Value then
						player.hidden.SpawnPoint.Value = tonumber(v.Name)
					end
				end
			end
		end)
	end
end

The Handler (for level transfering):

local function previousLevel()
	script.Parent.Handler.Click:Play()
	if CurrentValue.Value ~= 1 then
		CurrentValue.Value = CurrentValue.Value - 1
		player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value - 1
		Level.Text = CurrentValue.Value
		-- teleport
		Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
	end
end

local function previousLevel2()
	script.Parent.Handler.Click:Play()
	if CurrentValue.Value >= 11 then
		CurrentValue.Value = CurrentValue.Value - 10
		player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value - 10
		Level.Text = CurrentValue.Value
		-- teleport
		Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
	end
end

local function nextLevel()
	script.Parent.Handler.Click:Play()
	if (CurrentValue.Value + 1) <= player.leaderstats.Stage.Value then
		CurrentValue.Value = CurrentValue.Value + 1
		player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value + 1
		Level.Text = CurrentValue.Value
		-- teleport
		Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
	end
end

local function nextLevel2()
	script.Parent.Handler.Click:Play()
	if (CurrentValue.Value + 10) <= player.leaderstats.Stage.Value then
		CurrentValue.Value = CurrentValue.Value + 10
		player.hidden.SpawnPoint.Value = player.hidden.SpawnPoint.Value + 10
		Level.Text = CurrentValue.Value
		-- teleport
		Teleport:FireServer(checkpoints:FindFirstChild(tostring(CurrentValue.Value)))
	end
end


Previous.MouseButton1Click:Connect(function()
	previousLevel()
end)

Previous1.MouseButton1Click:Connect(function()
	previousLevel2()
end)

Next.MouseButton1Click:Connect(function()
	nextLevel()
end)

Next1.MouseButton1Click:Connect(function()
	nextLevel2()
end)

CurrentValue.Value = player:WaitForChild("leaderstats").Stage.Value
Level.Text = CurrentValue.Value

player.leaderstats.Stage:GetPropertyChangedSignal("Value"):Connect(function()
	CurrentValue.Value = player.leaderstats.Stage.Value
	Level.Text = CurrentValue.Value
end)

Let me know if you need more details, I’ve been working on this for 6 hours but hopefully this clears up confusion from previous posts I made on this since this issue has grown over the course of the 6 hours, no errors.

Edit: They’re all Int-Values…

1 Like