Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I cancel out inputs when i'm out of the seat?

I am trying to make a swing. This swing can be pushed by holding the W and S keys. However, the swing can still be pushed when i'm out of the seat.

local seat = script.Parent
seat.Changed:Connect(function(property)
	if property ~= "Occupant" then return end
	local occupant = seat.Occupant
	if occupant then
		local character = occupant.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			print(player.Name .. " is sitting")
			local keyHeld = false
			local thruster = Instance.new("BodyThrust", seat)
			local function keyPress(inputObject, gameProcessedEvent)
				if inputObject.KeyCode == Enum.KeyCode.W then
					keyHeld = true
					while keyHeld do
						thruster.Force = Vector3.new(0,0,-500)
						wait()
					end
				end
				if inputObject.KeyCode == Enum.KeyCode.D then
					keyHeld = true
					while keyHeld do
						thruster.Force = Vector3.new(0,0,500)
						wait()
					end
				end
			end
			local function keyRelease(inputObject, gameProcessedEvent)
				if inputObject.KeyCode == Enum.KeyCode.W then
					keyHeld = false
					thruster.Force = Vector3.new(0,0,0)
				end
				if inputObject.KeyCode == Enum.KeyCode.D then
					keyHeld = false
					thruster.Force = Vector3.new(0,0,0)
				end
			end
			game:GetService("UserInputService").InputBegan:Connect(keyPress)
			game:GetService("UserInputService").InputEnded:Connect(keyRelease)
		end
	end
end)

Any ideas?

2 answers

✓ Accepted answer

You'll have to Disconnect the bind once the user exits the seat. It can be used similarly to Connect.

void RBXScriptConnection:Disconnect()

local UserInputService = game:GetService("UserInputService")

if playerLeavesSeat then -- however you'd like to check when the player leaves the seat
	UserInputService.InputBegan:Disconnect()
	UserInputService.InputEnded:Disconnect()
end

The answer to this, is you have to separate the function connected with seat.Changed from the ones connected with events from UIS. Again, these should be separate entities.

The reason for doing this, is that you actually create countless functions because you define it inside the scope of someone sitting in the seat.

Next, we move some of the variables that you defined in that scope, to the most outer environment so all functions can see it.

Last, we create a boolean called "sitting", and make it true whenever the player sits, and false whenever they get up. Inside the functions powered by UIS, we check if this boolean is true before doing anything else.

local seat = script.Parent
local keyHeld = false
local thruster = Instance.new("BodyThrust", seat)
local sitting = false

seat.Changed:Connect(function(property)
	if property ~= "Occupant" then return end
	local occupant = seat.Occupant
	if occupant then
		local character = occupant.Parent
		local player = game.Players:GetPlayerFromCharacter(character)
		if player then
			print(player.Name .. " is sitting")
			sitting = true
		end
	else
		sitting = false
	end
end)


local function keyPress(inputObject, gameProcessedEvent)
	if sitting then
		if inputObject.KeyCode == Enum.KeyCode.W then
			keyHeld = true
			while keyHeld do
				thruster.Force = Vector3.new(0,0,-500)
				wait()
			end
		end
		if inputObject.KeyCode == Enum.KeyCode.D then
			keyHeld = true
			while keyHeld do
				thruster.Force = Vector3.new(0,0,500)
				wait()
			end
		end
	end
end
local function keyRelease(inputObject, gameProcessedEvent)
	if sitting then
		if inputObject.KeyCode == Enum.KeyCode.W then
			keyHeld = false
			thruster.Force = Vector3.new(0,0,0)
		end
		if inputObject.KeyCode == Enum.KeyCode.D then
			keyHeld = false
			thruster.Force = Vector3.new(0,0,0)
		end
	end
end
game:GetService("UserInputService").InputBegan:Connect(keyPress)
game:GetService("UserInputService").InputEnded:Connect(keyRelease)

This discussion is preserved as it was. New answers, comments, and votes are closed.