Issue with loop

Hello,

I’m trying to make a script that puts a platform under the player and moves along with the player to walk on. The Y value is supposed to originate at the characters current position in the world and only update X and Z values with the player as they walk (Works) but when you press Q or E it’s supposed to also raise the platform (Currently not working). I’m guessing it’s because in the while loop it’s grabbing the CFrame value that was initially set before the while loop, setting it to it over and over. Pressing a key just makes the screen twitch and the platform immediately returns to the position it was spawned in.
Any help appreciated!

		local charPos = Player.Character.HumanoidRootPart
		platform.Color = Color3.fromRGB(255, 0, 0) -- Creates platform properties
		platform.Size = Vector3.new(0.2,5,5)
		platform.Anchored = true
		platform.Material = Enum.Material.Neon
		platform.CFrame = charPos.CFrame * CFrame.new(0, -2.5, 0)
		platform.Shape = Enum.PartType.Cylinder
		while ghostmapEnabled == true do
			task.wait()
			platform.Parent = workspace
			local charRoot = Player.Character:WaitForChild("HumanoidRootPart")
			platform.CFrame = CFrame.new(charPos.CFrame.X, platform.CFrame.Y, charPos.CFrame.Z) * CFrame.Angles(0,0,math.rad(90)) -- Puts the platform under the character and rotates it as Cylinders spawn on their side... I think the issue is in this line. Sets platform Y position set from outside of loop.
			UIS.InputBegan:Connect(function(input) -- Keybinds supposed to raise and lower platform as it's already spawned. Currently just twitches screen and does not raise or lower.
				if input.KeyCode == Enum.KeyCode.Q then
					platform.CFrame = platform.CFrame * CFrame.new(0, 5, 0)
				else if input.KeyCode == Enum.KeyCode.E then
					platform.CFrame = platform.CFrame * CFrame.new(0, -5, 0)
					end
				end
			end)

This is because you are putting the InputBegan function within the loop. This means, everytime the loop runs it is essentially creating a new function, which is what is causing your issues. This also leads to severe memory leaks due to unused connections being creating every time the loop is cycled.

All you need to do is place the InputBegan function outside of the loop, and it should work as intended.