Script tool to NOT work when sitting in seat

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
  • I want my tool to not work at all when seated in a seat (ie. vehicle seat)
  1. What is the issue?
  • I do not know how to script this, as I am fairly new to scripting, I can get by here and there, but not enough to figure out this issue
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • I tried looking for this issue everywhere, all I have found (which I currently implement, see below) is a script that disables the tool when you first sit in the seat, but then the player can still use it after that. I want to disable the tool completely until they are out of the seat again.

Here is my current script, parented to the seat:

local Players = game:GetService(“Players”)
local Seat = script.Parent

Seat:GetPropertyChangedSignal(“Occupant”):Connect(function()
local humanoid = Seat.Occupant
if not humanoid then return end
humanoid:UnequipTools()
end)

Thanks for your help!

2 Likes

You can use Humanoid.Seated which is an event that fires when the player either sits or gets up from a seat.

https://developer.roblox.com/en-us/api-reference/event/Humanoid/Seated

You can disable/enable the tool when the event fires

So I have this code in a local script in StarterPlayerScripts:

local character = script.Parent
local humanoid = character:WaitForChild(“Humanoid”)

function onSeated(isSeated, seat)
if isSeated then
game.StarterPack[“Jet Pack”].Enabled = false
else
end

However, player can still equip and use the jet pack when seated.
end

humanoid.Seated:Connect(onSeated)

The tool isn’t in the starter pack, it’s in player.backpack. The starter pack is where the tool is cloned from to the players backpack. Try this:

game.Players:GetPlayerFromCharacter(character).Backpack["Jet Pack"].Enabled = false

You should place this in to the tool

local Player = game:GetService("Players").LocalPlayer
local Tool = script.Parent
local Character = Player.Character
local Seated = false

if not Character or not Character.Parent then
Character = Player.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.StateChanged:Connect(function(OldState, NewState)
       if NewState == Enum.HumanoidStateType.Seated then
              Seated = true
       elseif NewState ~= Enum.HumanoidStateType.Seated then
              Seated = false
       end
end)

-- Tool code here (use Seated variable to determine whether your tool works or not)

You can use that, the variable Seated will be true if they are sat, false if they are not, and you can use that variable to help you decide whether the tool should work or not.

I tried this:

local Player = game:GetService(“Players”).LocalPlayer

local Tool = script.Parent

local Character = Player.Character

local Seated = false

if not Character or not Character.Parent then

Character = Player.CharacterAdded:Wait()

end

local Humanoid = Character:WaitForChild(“Humanoid”)

Humanoid.StateChanged:Connect(function(OldState, NewState)

if NewState == Enum.HumanoidStateType.Seated then

Seated = true

elseif NewState ~= Enum.HumanoidStateType.Seated then

Seated = false

end

end)

– Tool code here (use Seated variable to determine whether your tool works or not)

while Seated == true do

wait()

Tool.Enabled = false

end

Doesn’t work though :frowning:

This also didn’t work for me, sorry guys i know i’m doing something wrong, im not that great with scripting

That’s because you didn’t do what I said. You should’ve coded the tool within the parameters of Seated == true,

like so

local UIS = game:GetService("UserInputService")
local Player = game:GetService("Players").LocalPlayer
local Tool = script.Parent
local Character = Player.Character
local Seated = false
local Equipped = false

if not Character or not Character.Parent then
Character = Player.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild("Humanoid")

Humanoid.StateChanged:Connect(function(OldState, NewState)
       if NewState == Enum.HumanoidStateType.Seated then
              Seated = true
       elseif NewState ~= Enum.HumanoidStateType.Seated then
              Seated = false
       end
end)

Tool.Equipped:Connect(function()
Equipped = true
end)

Tool.Unequipped:Connect(function()
Equipped = false
end)

UIS.InputBegan:Connect(function(Input, Processed)
if not Processed and Input.UserInputType == Enum.UserInputType.MouseButton1 and Equipped == true and Seated == false then
print("Player clicked")
end
end)

That will print “Player Clicked” everytime the player clicks WHILE the tool is equipped as long as they are not seated, and not clicking a GUI.

2 Likes

i get an error that says “attempt to index nil with ‘Character’” on line 4

Are you using a localscript for it?

1 Like

When i put Astral’s script into the tool, I used a regular script… however, when I tried my first script in the starter scripts i used a local script.

Use Astral’s script in a localscript, as .LocalPlayer on the player service inside a normal script, scripts cannot define the localplayer

1 Like

Got it! and then im following the if then statement with:
Player.Backpack[“Jet Pack”].Enabled = false

?

Yes, you check if the Jet Pack tool is inside the player’s backpack and if it is you can set the enabled property to false

1 Like

Yes that worked wonderfully, thank you all for your help, i’ve learned a lot.

Follow up question: Does everything remain the same if the tool doesnt begin in the starterpack? Once someone picks up the Jet Pack tool from the workspace, I will still be calling for it from the player’s backpack?

Yes, but if the player has it equipped it would be in the players character.

I see, that makes sense…

I ended up moving the tool to the workspace and putting a local script in it like this:

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer 
local Tool = script.Parent
local Character = Player.Character or Player.CharacterAdded:wait() 
local Seated = false
local Equipped = false
local mouse = Player:GetMouse()

if not Character or not Character.Parent then
	Character = Player.CharacterAdded:Wait()
end
local Humanoid = Character:WaitForChild('Humanoid') 

Humanoid.StateChanged:Connect(function(OldState, NewState)
	if NewState == Enum.HumanoidStateType.Seated then
		Seated = true
	elseif NewState ~= Enum.HumanoidStateType.Seated then
		Seated = false
	end
end)

Tool.Equipped:Connect(function()
	Equipped = true
end)

Tool.Unequipped:Connect(function()
	Equipped = false
end)

UIS.InputBegan:Connect(function(Input, Processed)
	if not Processed and Input.UserInputType == Enum.UserInputType.MouseButton1 and Equipped == true and Seated == true then
		Tool["Jetpack Local"].Disabled = true
	end
	if not Processed and Input.UserInputType == Enum.UserInputType.MouseButton1 and Equipped == true and Seated == false then
		Tool["Jetpack Local"].Disabled = false
	end
	if not Processed and Input.UserInputType == Enum.UserInputType.Touch and Equipped == true and Seated == true then
		Tool["Jetpack Local"].Disabled = true
	end
	if not Processed and Input.UserInputType == Enum.UserInputType.Touch and Equipped == true and Seated == false then
		Tool["Jetpack Local"].Disabled = false
	end

end)

Disabling the tool didn’t work any which way I tried it, so then i decided to disable the script inside of it instead and that worked. However, I don’t know if pointing to the tool directly is the right way, because although it works, sometimes it doesn’t work for some players. What do you think of this?