Respawn Rotation

I am creating an obby, which needs checkpoints. I am using an int value in the player and depending on what the int value is it sets the players’ spawn to the specified location. However I am using a cylinder part that is rotated, so when you spawn your character is rotated. Are there any ways around this?

The Script
local Players = game:GetService('Players')
local Obby = {}
Obby.Checkpoints = {}
Obby.Players = {}
function Obby:Touched(Character, Part)
	local Player = Players:GetPlayerFromCharacter(Character)
	local PlayerInfo = self.Players[Player and Player.UserId or 0]
	local CurrentLevel = PlayerInfo.Level
	local NextLevel = CurrentLevel + 1
	local StageInfo = self.Checkpoints[NextLevel] or {}
	if StageInfo.Part and StageInfo.Part == Part and (Character.PrimaryPart.Position-Part.Position).Magnitude<=8 then
		if tick() - PlayerInfo.LastLeveled < 3 and StageInfo.Level>=NextLevel then
			PlayerInfo.Counts = PlayerInfo.Counts + 1
			if PlayerInfo.Counts >= 3 then
				return Player:Kick('you went to next stage three times too fast ...')
			end
		end
		if StageInfo.Level == NextLevel then
			PlayerInfo.LastLeveled = tick()
			PlayerInfo.Stage.Value = NextLevel
			PlayerInfo.Level = NextLevel
		end
	end
end
function Obby:CreateStats(Player)
	local Stats = Instance.new('Folder')
	Stats.Name = 'leaderstats'
	Stats.Parent = Player
	local Level = Instance.new('IntValue')
	Level.Name = 'Stage'
	Level.Value = 0
	Level.Parent = Stats
	local Data = self.Players[Player.UserId]
	Level.Value = Data.Level
	Data.Stage = Level
end
function Obby.SetupCharacter(Character)
	wait()
	Character:WaitForChild'HumanoidRootPart'
	local Player = Players:GetPlayerFromCharacter(Character)
	local PlayerInfo = Obby.Players[Player.UserId]
	local Checkpoint = Obby.Checkpoints[PlayerInfo.Level]
	if Checkpoint then
		Character:SetPrimaryPartCFrame(Checkpoint.Part.CFrame*CFrame.new(0,3,0))
	end
end
function Obby.SetupPlayer(Player)
	local Data = Obby.Players[Player.UserId]
	Obby.Players[Player.UserId] = Data or {
		['Level'] = 0;
		['LastLeveled'] = 0;
		['Counts'] = 0;
	};
	local Character = Player.Character or Player.CharacterAdded:Wait()
	Obby.SetupCharacter(Character)
	Player.CharacterAdded:Connect(Obby.SetupCharacter)
	return not Data and Obby:CreateStats(Player)
end
function Obby.RemovePlayer(Player)
	local Data = Obby.Players[Player.UserId]
	if Data then
		Obby.Players[Player.UserId] = nil
	end
end
function Obby:SetupCheckpoint(Part)
	local Level = tonumber(Part.Name)
	self.Checkpoints[Level] = {
		['Part'] = Part;
		['Level'] = Level;
	}
	Part.Touched:Connect(function(Hit)
		local Character = Hit and Hit.Parent
		local Humanoid = Character and Character:FindFirstChildOfClass'Humanoid'
		if Humanoid and Humanoid.Health > 0 then
			self:Touched(Character, Part)
		end
	end)
end
Players.PlayerAdded:Connect(Obby.SetupPlayer)
Players.PlayerRemoving:Connect(Obby.RemovePlayer)
for _, Player in next, Players:GetPlayers() do
	Obby.SetupPlayer(Player)
end
local Checkpoints = workspace.Checkpoints
for _, Checkpoint in next, Checkpoints:GetChildren() do
	Obby:SetupCheckpoint(Checkpoint)
end
The Error

https://gyazo.com/6bcf9c5581783d4c205b823b8f002995

2 Likes

At this line you’re setting the HumanoidRootPart’s CFrame to the Checkpoint’s CFrame plus 3 on the y axis:

Character:SetPrimaryPartCFrame(Checkpoint.Part.CFrame*CFrame.new(0,3,0))

Remember that CFrame includes Rotation therefore you’re gaining the Checkpoint’s Rotation.
To counter this, set the CFrame Angles to 0 on each axis like so:

Character:SetPrimaryPartCFrame(Checkpoint.Part.CFrame * CFrame.new(0,3,0) * CFrame.Angles(0,0,0))

Alternatively, you could create a new CFrame using it’s Position:

Character:SetPrimaryPartCFrame(CFrame.new(Checkpoint.Part.Position) * CFrame.new(0,3,0))
1 Like