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)
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)
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)
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)
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.
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)