Moving a player using variables

So I’m trying to make a script which gives players the option to pay robux to teleport back to where they died and continue fighting enemies without any lossage whatsoever.

Every part of the script works fine (Logging the players coordinates, charging them the purchase, etc., etc.) up until the line of code which actually teleports the player back to where they were last after respawning.

I think I’ve traced it to the fact that, when the variables are plugged into the CFrame teleport, it just buggers up and breaks.

Here’s a simplified version of the server script which (I believe) the problem revolves around:


-- ALL VALUES ARE OMITTED FROM THE CODE SAMPLE TO KEEP IT SIMPLE, IT ISN'T A VALUES PROBLEM.

marketplace.ProcessReceipt = function(receiptinfo)
	if receiptinfo.ProductId == 1150196741 or receiptinfo.ProductId == 1150196820 or receiptinfo.ProductId == 1150196876 or receiptinfo.ProductId == 1150196958 then
		
		-- variables
		
		Purchases = Purchases + 1
		
		local player = game.Players:GetPlayerByUserId(receiptinfo.PlayerId)
		local character = player.Character
		
		player:LoadCharacter()
		
		-- Reset Respawn Timer
		
		CancelRespawn = true
		Respawntimer = 30
		
		print (DPX) -- This prints just fine
		print (DPY) -- As well as this
		print (DPZ) -- And this too
		
		-- Move player
		
		wait (.1)
		character.HumanoidRootPart.Position = CFrame.new(CFrame.new(Vector3.new(DPX, DPY + 10, DPZ))) -- Buggers up here
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

And the whole server script:

Full script
game.Players.CharacterAutoLoads = false -- Auto respawn

local RS = game:GetService("ReplicatedStorage")
local PopupsFolder = RS:WaitForChild(script.Name)

local RespawnReceiver = PopupsFolder.RespawnEvent
local ContinueReceiver = PopupsFolder.ContinueEvent

local Market = game:GetService("MarketplaceService")

game.Players.PlayerAdded:Connect(function(player)

	player:LoadCharacter()

end)

-- Values

local DPX = 0
local DPY = 0
local DPZ = 0

local CancelRespawn = false
local Respawntimer = 30

-- Purchases

local Purchases = 0

-- Payment Received:

local marketplace = game:GetService("MarketplaceService")

marketplace.ProcessReceipt = function(receiptinfo)
	if receiptinfo.ProductId == 1150196741 or receiptinfo.ProductId == 1150196820 or receiptinfo.ProductId == 1150196876 or receiptinfo.ProductId == 1150196958 then
		
		-- variables
		
		Purchases = Purchases + 1
		
		local player = game.Players:GetPlayerByUserId(receiptinfo.PlayerId)
		local character = player.Character
		
		player:LoadCharacter()
		
		-- Reset Respawn Timer
		
		CancelRespawn = true
		Respawntimer = 30
		
		print (DPX)
		print (DPY)
		print (DPZ)
		
		-- Move player
		
		wait (.1)
		character.HumanoidRootPart.Position = CFrame.new(CFrame.new(Vector3.new(DPX, DPY + 10, DPZ)))
		
		return Enum.ProductPurchaseDecision.PurchaseGranted
	end
end

--

game.Players.PlayerAdded:Connect(function(player)
	
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			
			DPX = character.UpperTorso.Position.X
			DPY = character.UpperTorso.Position.Y
			DPZ = character.UpperTorso.Position.Z
			
			local ClonedRespawn = script.Respawn:Clone()
			ClonedRespawn.Parent = player.PlayerGui:WaitForChild("Popups")
			
			local CancelRespawn = false
			local Respawntimer = 30
			
			local RootPart = character.HumanoidRootPart
			
			ContinueReceiver.OnServerEvent:Connect(function()
				
				if Purchases == 0 then

					game:GetService("MarketplaceService"):PromptProductPurchase(player, 1150196741)

				elseif Purchases == 1 then

					game:GetService("MarketplaceService"):PromptProductPurchase(player, 1150196820)

				elseif Purchases == 2 then

					game:GetService("MarketplaceService"):PromptProductPurchase(player, 1150196876)

				elseif Purchases >= 3 then

					game:GetService("MarketplaceService"):PromptProductPurchase(player, 1150196958)

				end
			end)
			
			RespawnReceiver.OnServerEvent:Connect(function()
				
				player:LoadCharacter()
				
				wait (.1)
				
				local Humanoid = player.Character.HumanoidRootPart
				Humanoid.CFrame = CFrame.new(Vector3.new(Humanoid.Position.X, 10, Humanoid.Position.Z))
				
			end)
			
			while Respawntimer >= 0 and CancelRespawn == false do
				
				if Respawntimer <= 0 then
					
					player:LoadCharacter()
					
					Respawntimer = 30
					
					CancelRespawn = true
					
					wait (.1)
					
					RootPart.Position = CFrame.new(CFrame.new(Vector3.new(RootPart.Position.X, 10, RootPart.Position.Z)))
					
				else
					Respawntimer = Respawntimer - 1
					
					wait (1)
					
					ClonedRespawn.Respawn.Text = "Respawn (" .. Respawntimer .. ")"
				end
			end
			
		end)
	end)
end)
1 Like

The variable that you initially create for the Character will be referencing their previous Character and not their most recent one because their Character is respawned immdediately afterwards. This means that when you try to move them after the Respawntimer:

From my understanding, this would not be teleporting the player’s current Character.

In order to resolve this, you can reference the player’s Character right before teleporting them to ensure that it exists:

local Character = player.Character or player.CharacterAdded:Wait()

Character:SetPrimaryPartCFrame(CFrame.new(CFrame.new(Vector3.new(DPX, DPY + 10, DPZ))))

Also, although I don’t know the values stored for DPX/Y/Z, I don’t think using that many constructors would be required to obtain the correct position to teleport the Character – it could probably be simplified to this:

Character:SetPrimaryPartCFrame(CFrame.new(DPX, DPY + 10, DPZ))
1 Like

Omg it works, thanks :smiley: :smiley: :smiley:

1 Like