Problem with player training

Hello developers!

I found a glitch and I don’t know how to fix it. When player presses “E” and instantly teleports to ex. Desert then player can’t move and stop training until his energy is lower then required energy.

This is how it looks:

Script:

local function checkTreadmills()
	for i, v in pairs(Worlds:GetDescendants()) do
		if v:FindFirstChild("Treadmills") then
			return true
		end
	end
end

--------------------------------------------------------

--[[Training]]--

RemotesFolder.Train.OnServerEvent:Connect(function(plr)
	local char = plr.Character
	local hum = char.Humanoid
	local animator = hum.Animator
	local anim = script.Animation
	
	local track = animator:LoadAnimation(anim)
	

	if checkTreadmills() then
		for i, treadmill in pairs(Worlds:GetDescendants()) do
			if treadmill.Name == "bierznia" then
				if (plr.Character.Head.Position - treadmill.Hitbox.Position).Magnitude < 5 then
					if plr.values.Req.Value <= plr.leaderstats.Energy.Value then
						if plr.values.Training.Value == false then
							switchTp(plr, false)
							plr.values.Training.Value = true
							print("Training")
							plr.Character.Humanoid.WalkSpeed = 0
							plr.Character.Head.CFrame = treadmill.Teleport.CFrame
							plr.Character.Humanoid.JumpHeight = 0
							plr.CameraMinZoomDistance = 1
							track:Play()
							track:AdjustSpeed(2.5)
							wait(2)
							plr.values.Training.Value = plr.values.Training.Value
							repeat
								wait(1.5)
								if plr.leaderstats.Energy.Value < plr.values.Req.Value or plr.values.Training.Value == false then
									break
								end
								plr.leaderstats.Speed.Value += 1
								plr.leaderstats.Energy.Value -= plr.values.Req.Value 
								plr.values.Req.Value = plr.values.Req.Value + 4.5
								plr.values.Training.Value = plr.values.Training.Value
							until plr.leaderstats.Energy.Value < plr.values.Req.Value or plr.values.Training.Value == false
							track:Stop(0)
							plr.CameraMinZoomDistance = 0.5
							plr.values.Training.Value = false
							plr.Character.Humanoid.WalkSpeed = 16
							plr.Character.Humanoid.JumpHeight = 7.2
							switchTp(plr, true)
						else
							plr.CameraMinZoomDistance = 0.5
							track:Stop(0)
							plr.values.Training.Value = false
							plr.Character.Humanoid.WalkSpeed = 16
							plr.Character.Humanoid.JumpHeight = 7.2
							switchTp(plr, true)
						end
					end
				end
			end
		end
	end
end)

I tried with Magnitude in loop

I tried to solve it but nah…

This text will be blurred

you just need to check if the player is close enough to train, maybe 5 / 10 studs would be good… every time you try to add things and remove stamina just add that check, have it as a function, would be “cleaner”

if checkTreadmills() then
		for i, treadmill in pairs(Worlds:GetDescendants()) do
			if treadmill.Name == "bierznia" then
				if (plr.Character.Head.Position - treadmill.Hitbox.Position).Magnitude < 5 then
					if plr.values.Req.Value <= plr.leaderstats.Energy.Value then
						if plr.values.Training.Value == false then

Maybe its because the script is getting stuck in the repeat loop and cannot access any part outside of the script. If this is the case you should make another thread to check.