Disabling the Toolbar when a player is seated

I have been trying to create a simple script that automatically disables the toolbar when a player is seated. I have found no luck from all the script I have tried. The scripts that I have tried in the past, I have placed them in StarterPlayerScripts or StarterPack.

My script below:


local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local backpack = game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack)


game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local function checkSeated()
	if character.Humanoid.Seated then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	end
end


checkSeated()


character.Humanoid.Seated:Connect(function(isSeated)
	if isSeated then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	else
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

	end
end)


1 Like

I don’t think you can disable CoreGuis on the server, try using a LocalScript.

I did try a LocalScript, but found myself without luck.

You’re using Coregui on a server script which wouldn’t work plus they’ve been alot of bugs lately on Roblox studio.

try this code:

Put this in a local script and put the local script in starterplayerscripts

local Players = game:GetService("Players")
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")

Humanoid.Seated:Connect(function(isSeated, Part)
	if isSeated then
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
	else
		game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
	end
end)
2 Likes

There are a couple of issues with your script:

The backpack variable is not being used, so it can be removed.
The checkSeated() function is only being called once at the start of the script, so it won’t continuously check if the player is seated or not.
The game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false) line is being called twice, so you can remove the first one.
Here’s an updated version of your script:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)

local function checkSeated()
    if character.Humanoid.Seated then
        game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)
    else
        game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, true)
    end
end

character.Humanoid.Seated:Connect(checkSeated)

This script will continuously check if the player is seated or not and enable/disable the backpack accordingly. It also removes the unnecessary if statement inside the character.Humanoid.Seated event handler.

Suprisingly, the solution actually did not work even though I believed it did. The toolbar still shows when a player is seated.

This script should be played in StarterPlayerScripts, correct?

Not sure why that didn’t work, but here’s a slightly different method that works:

local starterGui = game:GetService('StarterGui')
local player = game:GetService('Players').LocalPlayer
local humanoid

player.CharacterAdded:Connect(function(character)
	humanoid = character:WaitForChild('Humanoid')
	humanoid:GetPropertyChangedSignal('Sit'):Connect(function()
		if humanoid.Sit then
			starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,false)
			local tool = character:FindFirstChildWhichIsA('Tool')
			if tool then
				tool.Parent = player.Backpack
				tool = nil
			end
		else
			starterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack,true)
		end
	end)
end)

This will also move the player’s equipped tool back into the backpack if they have it out while sitting.

(This is a LocalScript in StarterPlayerScripts of course.)

3 Likes

Oh nevermind. I could have used GetPropertyChangedSignal to check the sit property inside the character’s humanoid. I keep forgetting about that event.

@Michyays, this might be the working solution for your problem.

This is why AI is not ready to write code yet. It’ll often spout nonsense that doesn’t exist in the first place. But in this case, while the code is mostly correct, it’s inefficient. You should avoid using loops as much as possible, especially when there are events that work perfectly for what you’re trying to do.

The code there also disables the backpack at the start, which was not asked for. Then with using CharacterAdded:Wait(), this would only work if the script were in the StarterCharacterScripts folder, not StarterPlayerScripts, as this would only fire once and break the moment the player respawns. It also doesn’t do any garbage collection preparation or a proper loop close, not that a loop should be used in this case anyway.

Your solution worked perfectly, thank you so much for your help.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.