Player Pos not updating to LocalScript

I am trying to create a system where you can save your player position and teleport back to it when pressing a key. It works fine when “enabled” is set to true, but after it is set to false and then true new checkpoints are put in a random place (all in the same place) and I cannot teleport to them. Why is this happening and how can I fix it? I think the issue may be the title.

game:GetService("UserInputService").InputBegan:Connect(function(input, gameProcessedEvent)
	print("pressed")
	if enabled then
			if not gameProcessedEvent then
		
			if input.KeyCode == New then
				local Clone = root:Clone()
				for i,v in Clone:GetChildren() do
					if v.Name == "RootJoint" then
						v:Remove()
					end
				end
				Clone.Color = Color3.new(1,1,1)
				Clone.Transparency = .8
				Clone.Material = Enum.Material.Neon
				Clone.Name = tostring(i)
				Clone.Anchored = true
				Clone.Parent = Parts
			i+=1
			print(Clone)
			end
			
			if input.KeyCode == Back then
				local lastCP = 0
				for i,v in Parts:GetChildren() do
					if tonumber(v.Name) > lastCP then
						lastCP = tonumber(v.Name)
					end
				end
				root.CFrame = Parts[lastCP].CFrame
			end
			
			if input.KeyCode == Delete then
				local lastCP = 0
				for i,v in Parts:GetChildren() do
					if tonumber(v.Name) > lastCP then
						lastCP = tonumber(v.Name)
					end
				end
				Parts[lastCP]:Remove()
			end
		end
	end
end)
1 Like

Sorry if this is a dumb question, but what exactly is this code supposed to achieve? Is this for when the player teleports or is it something different?

This creates a new checkpoint for the player to teleport to. That’s not the part of the code that I’m confused about, though. It functions perfectly until I disable and then re-enable the “enabled” variable.

In the ‘new’ event, I don’t see anywhere that sets the objects position

alright thank you for clearing that up.

The code creates a clone of the player’s HumanoidRootPart (I’m assuming that’s what root means) and you probably know that clones have all of the same attributes (including position) so the object should be set to the player’s position.

The only reason I can think of that would cause this is that somehow the HRP’s position isn’t where it should be, are you altering it in any other scripts?

You should consider making the player’s position change in a server script and not in a LocalScript.

The position is already set to the position of the player’s root part. I tried adding a line that would reset the block’s position to the position of the player’s root part and that did nothing either.

I can’t do that because I need the part to be in the player’s workspace.

The “root” variable was not being reset when the player died.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.