I made a script and it's not working properly

I am currently trying to make a script where when you activate the proximity prompt you’re character is frozen and you get energy while in that state but if you jump it stops giving you energy and unfreezes your character allowing to move again.

local animationId = script.Parent.Animation
local animationPlaying = false
local energyGainRate = 1
local energyTimer = nil

local tpPart = script.Parent.Parent.Parent.Inside
local outPart = script.Parent.Parent.Parent.OUT

ProximityPrompt.Triggered:Connect(function(player)
	if not animationPlaying then
		local humanoid = player.Character:FindFirstChild("Humanoid")
		if humanoid then
			local leaderstats = player:FindFirstChild("leaderstats")

			local energyValue = leaderstats:FindFirstChild("Energy")

			local anim = humanoid:LoadAnimation(animationId)
			anim:Play()

			local humanoidRootPart = humanoid.RootPart
			humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false) 
			humanoidRootPart.CFrame = tpPart.CFrame 

			local elapsedTime = 0
			energyTimer = game:GetService("RunService").Heartbeat:Connect(function(deltaTime)
				elapsedTime = elapsedTime + deltaTime
				if elapsedTime >= 1 then
					local energyToAdd = math.floor(elapsedTime)
					energyValue.Value = energyValue.Value + energyToAdd
					elapsedTime = elapsedTime - energyToAdd
				end
			end)

			animationPlaying = true

			local function onJump()
				if animationPlaying then
					anim:Stop()

					humanoidRootPart.AssemblyLinearVelocity = Vector3.new(0, 0, 0) 
					humanoidRootPart.CFrame = outPart.CFrame 

					if energyTimer then
						energyTimer:Disconnect()
						energyTimer = nil
					end

					animationPlaying = false
				end
			end

			humanoid.Jumping:Connect(onJump)
			humanoid.FreeFalling:Connect(onJump)
		end
	end
end)

game:GetService("Players").PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function()
			if energyTimer then
				energyTimer:Disconnect()
				energyTimer = nil
			end
			animationPlaying = false
		end)
	end)
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
	if energyTimer then
		energyTimer:Disconnect()
		energyTimer = nil
	end
	animationPlaying = false
end)```

For some reason you are able to move and still gives you energy, I wanna fix that.

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