Hold to Jump Script

I’m trying to make a script + GUI of a system where you have to hold down the space button to confirm that you want to jump (to avoid jumping off by accident)

However I’m having some specific problems.

1: after you jump out of the seat and walk around, the GUI still appears and the tween still play when you press the spacebar, even though though they are nested inside the seat’s GetPropertyChangedSignal event

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Humanoid = Seat.Occupant
	
	--if there is no humanoid occupant then..
	if not Humanoid then
		ScreenGui.Enabled = false
		return
	end
	
	--if there is humanoid occupant then..
	if Humanoid then
		print("localscript detected humanoid sitting")

		UIS.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Space then
				ScreenGui.Enabled = true

				--create tween
				--(this part taken out for readability purposes)
				TSize:Play()
				
				--detect when tween cancelled or completed
				local playbackChanged = TSize:GetPropertyChangedSignal("PlaybackState")
				
				local function onPlaybackChanged()
					if TSize.PlaybackState == Enum.PlaybackState.Cancelled then
						print("tween cancelled")
					elseif TSize.PlaybackState == Enum.PlaybackState.Completed then
						--humanoid can jump out of the seat now
						print("tween completed")
						ScreenGui.Enabled = false
						Humanoid.Sit = false
						Humanoid.JumpPower = 50
					end
				end
				
				playbackChanged:Connect(onPlaybackChanged)
			end
		end)

2: When you jump out of the seat, then sit down again, and then try to jump out again, you immediately jump out, although the script will set the humanoid’s jump power to 0 when a humanoid is sitting on it. Below is the full server script located under the seat object

local Seat = script.Parent

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Humanoid = Seat.Occupant
	if not Humanoid then print("no humanoid") return end --if no humanoid occupant end
	
	if Humanoid then --if there is humanoid occupant then..
		print("serverscript detected humanoid sitting. Humanoid's jump power should be zero")
		Humanoid.UseJumpPower = true
		Humanoid.JumpPower = 0
	end
end)

The full local script: StarterGui.ScreenGui.LocalScript

local Seat = game.Workspace:WaitForChild("Seat")
local ScreenGui = game.Players.LocalPlayer.PlayerGui.ScreenGui
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

ScreenGui.Enabled = false

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	local Humanoid = Seat.Occupant
	
	--if there is no humanoid occupant then..
	if not Humanoid then
		ScreenGui.Enabled = false
		return
	end
	
	--if there is humanoid occupant then..
	if Humanoid then
		print("localscript detected humanoid sitting")

		UIS.InputBegan:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Space then
				ScreenGui.Enabled = true
				
				local LoaderGui = ScreenGui.Loader

				--create tween
				LoaderGui.Size = UDim2.new(0, 0, 0.09, 0)
				
				local TInfo = TweenInfo.new(
					2, Enum.EasingStyle.Sine,
					Enum.EasingDirection.Out
				)
				
				TSize = TS:Create(LoaderGui, TInfo, {Size = UDim2.new(0.2,0,0.09,0)})
				TSize:Play()
				
				--detect when tween cancelled or completed
				local playbackChanged = TSize:GetPropertyChangedSignal("PlaybackState")
				
				local function onPlaybackChanged()
					if TSize.PlaybackState == Enum.PlaybackState.Cancelled then
						print("tween cancelled")
					elseif TSize.PlaybackState == Enum.PlaybackState.Completed then
						print("tween completed")
						ScreenGui.Enabled = false
						Humanoid.Sit = false
						Humanoid.JumpPower = 50
					end
				end
				
				playbackChanged:Connect(onPlaybackChanged)
			end
		end)
		UIS.InputEnded:Connect(function(input)
			if input.KeyCode == Enum.KeyCode.Space then
				ScreenGui.Enabled = false
				TSize:Cancel()
			end
		end)
	end
end)

Would it be easier to understand if you actually played the game? If so I can give link.

bumped cause i still need help with this…

I can promise anything but try this code.

local Seat = game.Workspace:WaitForChild("Seat")
local ScreenGui = game.Players.LocalPlayer.PlayerGui.ScreenGui
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

ScreenGui.Enabled = false
local TSize -- Define TSize outside of the event handler to access it later for cancellation

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local Humanoid = Seat.Occupant
    
    -- Disconnect InputBegan event when there's no occupant
    if not Humanoid then
        UIS.InputBegan:Disconnect()
        ScreenGui.Enabled = false
        return
    end
    
    print("localscript detected humanoid sitting")

    UIS.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.Space then
            ScreenGui.Enabled = true
            
            local LoaderGui = ScreenGui.Loader

            -- Create or reset tween
            LoaderGui.Size = UDim2.new(0, 0, 0.09, 0)
            local TInfo = TweenInfo.new(
                2, Enum.EasingStyle.Sine,
                Enum.EasingDirection.Out
            )
            if TSize then TSize:Cancel() end -- Cancel previous tween if it exists
            TSize = TS:Create(LoaderGui, TInfo, {Size = UDim2.new(0.2,0,0.09,0)})
            TSize:Play()
            
            -- Detect when tween cancelled or completed
            local playbackChanged = TSize:GetPropertyChangedSignal("PlaybackState")
            
            local function onPlaybackChanged()
                if TSize.PlaybackState == Enum.PlaybackState.Cancelled then
                    print("tween cancelled")
                elseif TSize.PlaybackState == Enum.PlaybackState.Completed then
                    print("tween completed")
                    ScreenGui.Enabled = false
                    Humanoid.Sit = false
                    Humanoid.JumpPower = 50
                end
            end
            
            playbackChanged:Connect(onPlaybackChanged)
        end
    end)
    UIS.InputEnded:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.Space then
            ScreenGui.Enabled = false
            if TSize then TSize:Cancel() end -- Cancel tween on spacebar release
        end
    end)
end)

Thanks for the response. I was able to solve the issue with the GUI appearing again, but now I’m trying to figure out how to make the tween play after you have jumped out and then sat down again. Do you possibly have ideas?

Yes, use this code right here

local Seat = game.Workspace:WaitForChild("Seat")
local ScreenGui = game.Players.LocalPlayer.PlayerGui.ScreenGui
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")

ScreenGui.Enabled = false
local TSize -- Define TSize outside of the event handler to access it later for cancellation

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
    local Humanoid = Seat.Occupant
    
    -- Disconnect InputBegan event when there's no occupant
    if not Humanoid then
        UIS.InputBegan:Disconnect()
        ScreenGui.Enabled = false
        return
    end
    
    print("localscript detected humanoid sitting")

    UIS.InputBegan:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.Space then
            ScreenGui.Enabled = true
            
            local LoaderGui = ScreenGui.Loader

            -- Create or reset tween
            LoaderGui.Size = UDim2.new(0, 0, 0.09, 0)
            local TInfo = TweenInfo.new(
                2, Enum.EasingStyle.Sine,
                Enum.EasingDirection.Out
            )
            if TSize then TSize:Cancel() end -- Cancel previous tween if it exists
            TSize = TS:Create(LoaderGui, TInfo, {Size = UDim2.new(0.2,0,0.09,0)})
            TSize:Play()
            
            -- Detect when tween cancelled or completed
            local playbackChanged = TSize:GetPropertyChangedSignal("PlaybackState")
            
            local function onPlaybackChanged()
                if TSize.PlaybackState == Enum.PlaybackState.Cancelled then
                    print("tween cancelled")
                elseif TSize.PlaybackState == Enum.PlaybackState.Completed then
                    print("tween completed")
                    ScreenGui.Enabled = false
                    Humanoid.Sit = false
                    Humanoid.JumpPower = 50
                end
            end
            
            playbackChanged:Connect(onPlaybackChanged)
        end
    end)
    UIS.InputEnded:Connect(function(input)
        if input.KeyCode == Enum.KeyCode.Space then
            ScreenGui.Enabled = false
            if TSize then TSize:Cancel() end -- Cancel tween on spacebar release
        end
    end)
end)