How to make LocalScript only work when in a seat

I have a script that controls a spotlight using LocalScript.

Is there any way I can make it so the script only starts to work when I’m sitting in a seat?

Here is the code:

local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SpotlightHead = game.Workspace.Spotlight.Head
local UpdateCon

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end

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

	UpdateCon = RunService.Heartbeat:Connect(function(dt)
		local newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(0.1, 0, 0)
		SpotlightHead:SetPrimaryPartCFrame(newCFrame)
	end)
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end
	if not UpdateCon then return end

	UpdateCon:Disconnect()
	UpdateCon = nil
end)

Any help is appreciated! :slightly_smiling_face:

Remote event:FireClient() is useful …

There’s an event called Humanoid.Seated, it fires when the character is seated or not. So if you set a variable to true when the humanoid is seated, and false when not, you can check it when you want.

Example from api reference:

local character = script.Parent
local humanoid = character:WaitForChild("Humanoid")
 
function onSeated(isSeated, seat)
	if isSeated then
		print("I'm now sitting on: " .. seat.Name .. "!")
	else
		print("I'm not sitting on anything")
	end
end
humanoid.Seated:Connect(onSeated)

And in the 12 th line you should use math.rad for CFrame angles???

add a variable at the start of your code for the player

local player = game.Players.LocalPlayer

then inside the first function add this if statement (make sure to set “Seat” to the actual seat you want the player to be sitting in)

if Seat.Occupant == player:WaitForChild("Character").Humanoid then

I tried to apply this but it broke the script.

Heres the code:

local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SpotlightHead = game.Workspace.Spotlight1.Arm.Head
local player = game.Players.LocalPlayer
local Seat = game.Workspace.Seat
local UpdateCon

if Seat.Occupant == player:WaitForChild("Character").Humanoid then

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end

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

	UpdateCon = RunService.Heartbeat:Connect(function(dt)
		local newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(-0.05, 0, 0)
		SpotlightHead:SetPrimaryPartCFrame(newCFrame)
	end)
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end
	if not UpdateCon then return end

	UpdateCon:Disconnect()
	UpdateCon = nil
end)

Try this instead:

local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SpotlightHead = game.Workspace.Spotlight1.Arm.Head
local player = game.Players.LocalPlayer
local Seat = game.Workspace.Seat
local UpdateCon

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    if not Seat.Occupant == player:WaitForChild("Character").Humanoid then return end
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end

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

	UpdateCon = RunService.Heartbeat:Connect(function(dt)
		local newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(-0.05, 0, 0)
		SpotlightHead:SetPrimaryPartCFrame(newCFrame)
	end)
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    if not Seat.Occupant == player:WaitForChild("Character").Humanoid then return end
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end
	if not UpdateCon then return end

	UpdateCon:Disconnect()
	UpdateCon = nil
end)

Yeah, the if statement needs to go inside the function, not before it!

A:

Since you mentioned that you don’t want the script to run only when your mouse enters the seat Specifically…
You can use @. to get the name of the player who sits on the seat. Then use game.Players.LocalPlayer.Name to get your name. Then use ~= to see if they match, == checks that they are exactly the same. ~= checks to see if they are the same value, but they can be stored in different locations.
if @.Occupant ~= game.Players.LocalPlayer.Name then
print(“You are not in the seat!”)
end

Also, make sure that you are running the script on the server and not with localscript, or else this will not work.

~= checks that the value on the left side of the expression is not equal to the right side.

This is incorrect. ~= checks that the value on the left side of the expression is equal to the right side.

This did not work.

Are you sure the first one should be if NOT?

[EDIT] I tried switching the first one to be without the “not”, and it didn’t work either.

Incorrect, == checks that the value on the left is equal to the value on the right. ~= is the not equals operator. If you don’t believe me check this:

You’re wrong, and you’re silly. == checks that the value on the left is equal to the value on the right.

This is exactly what I said first of all. Also, I’m not wrong. ~= is the not equals operator

Are there any errors showing in your output?

Also, ‘silly’ is pretty childish.

You’re an idiot.

That’s not very nice.

So what is the other operator?

The other operator is called ~= , which stands for ‘not equal to’.

You know what, if you want to troll then leave the forum. This is not the place for trolling and wasting peoples time.

It says, Infinite yield possible on ‘Players.Elvinwoman:WaitForChild(“Character”)’

[EDIT] it means that it is checking for something constantly

Infinite yield occurs usually during the use of WaitForChild this is when the script will infinitely yield aka infinitely wait because it can’t find the child that your looking for.

To fix this make your script this:

local userInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local SpotlightHead = game.Workspace.Spotlight1.Arm.Head
local player = game.Players.LocalPlayer
local Seat = game.Workspace.Seat
local UpdateCon

userInputService.InputBegan:Connect(function(input, gameProcessedEvent)
    local char = player.Character or player.CharacterAdded:Wait()
    if not Seat.Occupant == char.Humanoid then return end
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end

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

	UpdateCon = RunService.Heartbeat:Connect(function(dt)
		local newCFrame = SpotlightHead.PrimaryPart.CFrame * CFrame.Angles(-0.05, 0, 0)
		SpotlightHead:SetPrimaryPartCFrame(newCFrame)
	end)
end)

userInputService.InputEnded:Connect(function(input, gameProcessedEvent)
    local char = player.Character or player.CharacterAdded:Wait()
    if not Seat.Occupant == char.Humanoid then return end
	if input.UserInputType ~= Enum.UserInputType.Keyboard then return end
	if input.KeyCode ~= Enum.KeyCode.W then return end
	if not UpdateCon then return end

	UpdateCon:Disconnect()
	UpdateCon = nil
end)