Need help with CFrame

I’m making a game about streaming. When the player runs out of energy, and presses the end stream button, they have the same code. Though, when the player runs out of energy it works perfectly, and the end stream button makes the players’ cframe 0,0,0. I have no clue why this happens, and it’s most likely something very obvious I’ve missed. Thanks.

local live = game.ReplicatedStorage.StartStream
live.OnServerEvent:Connect(function(plr,isLive,what)
	
	local function format_number(amount)
		local formatted = amount
		while wait() do
			formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)","%1,%2")
			if (k==0) then
				break
			end
		end
		return formatted
	end


	
	local TOHX
	local TOHY
	local TOHZ

	local oldX
	local oldY
	local oldZ

	
	
	local stream1 = plr.StreamStats.Stream1
	local stream2 = plr.StreamStats.Stream2
	local stream3 = plr.StreamStats.Stream3
	local stream4 = plr.StreamStats.Stream4
	local stream5 = plr.StreamStats.Stream5
	local stream6 = plr.StreamStats.Stream6
	local stream7 = plr.StreamStats.Stream7
	local stream8 = plr.StreamStats.Stream8
	local stream9 = plr.StreamStats.Stream9
	local stream10 = plr.StreamStats.Stream10
	local stream11 = plr.StreamStats.Stream11
	local stream12 = plr.StreamStats.Stream12
	local stream13 = plr.StreamStats.Stream13
	local stream14 = plr.StreamStats.Stream14

	local humanoid = plr.Character:WaitForChild("Humanoid")
	if plr.Stuff.isLive.Value == false then
		local e = game.Lighting.Loading:Clone()
		e.Parent = plr.PlayerGui
		wait(.5)
		plr.PlayerGui.PC.Enabled = false
		if what == 1 then
			oldX = humanoid.RootPart.Position.X
			oldY = humanoid.RootPart.Position.Y
			oldZ = humanoid.RootPart.Position.Z
			if plr.Stuff.GravityCoil.Value == true then
				local gc = game.ReplicatedStorage.PlayGameDLC["Gravity Coil"]:Clone()
				gc.Parent = plr.Backpack
				end
			if plr.Stuff.SpeedCoil.Value == true then
				local gc = game.ReplicatedStorage.PlayGameDLC["Speed Coil"]:Clone()
				gc.Parent = plr.Backpack
			end
			if TOHX ~= nil and TOHY ~= nil and TOHZ ~= nil then
				plr.Character:FindFirstChildOfClass("Humanoid").Jump = true
				wait(1)
				
							oldX = humanoid.RootPart.Position.X
			oldY = humanoid.RootPart.Position.Y
			oldZ = humanoid.RootPart.Position.Z
				humanoid.RootPart.CFrame = CFrame.new(TOHX,TOHY,TOHZ)
			else
				plr.Character:FindFirstChildOfClass("Humanoid").Jump = true
				wait(1)
				
				oldX = humanoid.RootPart.Position.X
				oldY = humanoid.RootPart.Position.Y
				oldZ = humanoid.RootPart.Position.Z
				humanoid.RootPart.CFrame = CFrame.new(game.Workspace.Games.TOH.TheTP.CFrame.Position)
			end
		end
		plr.Stuff.isLive.Value = true
		plr.Stuff.NewComments.Value = 0
		plr.Stuff.NewFollowers.Value = 0
		plr.Stuff.NewSubscribers.Value = 0
		plr.Stuff.NewTime.Value = 0
		while plr.Stuff.isLive.Value == true and plr.Stuff.Energy.Value >= 1 do
			wait(0.33333333333333)
			plr.Stuff.Energy.Value -= 1
		end
		if plr.Stuff.Energy.Value == 0 then --THE PLAYER RAN OUT OF ENERGY, THE CODE WORKS PERFECTLY
			plr.Stuff.isLive.Value = false
			local e = game.Lighting.Loading:Clone()
			e.Parent = plr.PlayerGui
			wait(.5)
			local humanoid = plr.Character:WaitForChild("Humanoid")
			local tools = plr.Backpack:GetChildren()
			for i,tool in pairs(tools) do
				if tool:IsA("Tool") then
					if tool.Name == "Speed Coil" or tool.Name == "Gravity Coil" then
						tool:Destroy()
					end
				end
			end
			local tools = plr.Character:GetChildren()
			for i,tool in pairs(tools) do
				if tool:IsA("Tool") then
					if tool.Name == "Speed Coil" or tool.Name == "Gravity Coil" then
						tool:Destroy()
					end
				end
			end
			TOHX = humanoid.RootPart.Position.X
			TOHY = humanoid.RootPart.Position.Y
			TOHZ = humanoid.RootPart.Position.Z
			humanoid.RootPart.CFrame = CFrame.new(oldX,oldY,oldZ)
			plr.PlayerGui.StreamStats.Frame.Revenue.NewFollowers.Text = "$"..format_number(plr.Stuff.NewComments.Value)
			plr.PlayerGui.StreamStats.Frame.Followers.NewFollowers.Text = format_number(plr.Stuff.NewFollowers.Value)
			plr.PlayerGui.StreamStats.Frame.Subscribers.NewFollowers.Text = format_number(plr.Stuff.NewSubscribers.Value)
			plr.PlayerGui.StreamStats.Frame.Peak.NewFollowers.Text = format_number(plr.Stuff.NewTime.Value)
			plr.Stuff.StreamCount.Value += 1
			stream14.Value = stream13.Value
			stream13.Value = stream12.Value
			stream12.Value = stream11.Value
			stream11.Value = stream10.Value
			stream10.Value = stream9.Value
			stream9.Value = stream8.Value
			stream8.Value = stream7.Value
			stream7.Value = stream6.Value
			stream6.Value = stream5.Value
			stream5.Value = stream4.Value
			stream4.Value = stream3.Value
			stream3.Value = stream2.Value
			stream2.Value = stream1.Value
			stream1.Value = plr.Stuff.NewFollowers.Value..";"..plr.Stuff.NewTime.Value..";"..plr.Stuff.NewSubscribers.Value..";"..plr.Stuff.NewComments.Value			
			game.ReplicatedStorage.Toggle:FireClient(plr)			
		end
	else --THE PLAYER PRESSED THE END STREAM BUTTON, IT'S THE SAME CODE AS THE NO ENERGY BUT MAKES THE PLAYERS' CFRAME 0,0,0.
		plr.Stuff.isLive.Value = false
		local e = game.Lighting.Loading:Clone()
		e.Parent = plr.PlayerGui
		wait(.5)
		local humanoid = plr.Character:WaitForChild("Humanoid")
		local tools = plr.Backpack:GetChildren()
		for i,tool in pairs(tools) do
			if tool:IsA("Tool") then
				if tool.Name == "Speed Coil" or tool.Name == "Gravity Coil" then
					tool:Destroy()
				end
			end
		end
		local tools = plr.Character:GetChildren()
		for i,tool in pairs(tools) do
			if tool:IsA("Tool") then
				if tool.Name == "Speed Coil" or tool.Name == "Gravity Coil" then
					tool:Destroy()
				end
			end
		end

		TOHX = humanoid.RootPart.Position.X
		TOHY = humanoid.RootPart.Position.Y
		TOHZ = humanoid.RootPart.Position.Z
		humanoid.RootPart.CFrame = CFrame.new(oldX,oldY,oldZ)
		plr.PlayerGui.StreamStats.Frame.Revenue.NewFollowers.Text = "$"..format_number(plr.Stuff.NewComments.Value)
		plr.PlayerGui.StreamStats.Frame.Followers.NewFollowers.Text = format_number(plr.Stuff.NewFollowers.Value)
		plr.PlayerGui.StreamStats.Frame.Subscribers.NewFollowers.Text = format_number(plr.Stuff.NewSubscribers.Value)
		plr.PlayerGui.StreamStats.Frame.Peak.NewFollowers.Text = format_number(plr.Stuff.NewTime.Value)
		plr.Stuff.StreamCount.Value += 1
		stream14.Value = stream13.Value
		stream13.Value = stream12.Value
		stream12.Value = stream11.Value
		stream11.Value = stream10.Value
		stream10.Value = stream9.Value
		stream9.Value = stream8.Value
		stream8.Value = stream7.Value
		stream7.Value = stream6.Value
		stream6.Value = stream5.Value
		stream5.Value = stream4.Value
		stream4.Value = stream3.Value
		stream3.Value = stream2.Value
		stream2.Value = stream1.Value
		stream1.Value = plr.Stuff.NewFollowers.Value..";"..plr.Stuff.NewTime.Value..";"..plr.Stuff.NewSubscribers.Value..";"..plr.Stuff.NewComments.Value			
		game.ReplicatedStorage.Toggle:FireClient(plr)			
	end
	
end)


This part is the problem.
Every time the script runs, you create new variables, meaning that their values will be null (nothing or 0). So even tho you try saving the coordinates of the HumanoidRootPart, it doesn’t save, and when you try loading them back, you just get 0.

So in order for this to work, you should save the values outside of the function (outside of the live.OnServerEvent). I recommend you save the values in a table or dictionary with the player UIDs so multiple players can use it.

Another thing I noticed, you don’t have to save all 3 values of a coordinate differently. There are things called Vector3 that save all 3 values for you at the same time. Please read some articles about it
https://create.roblox.com/docs/reference/engine/datatypes/Vector3
https://create.roblox.com/docs/scripting/luau/tables#dictionaries

1 Like

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