How to Check if a Player is Sitting?

This is problem a easy fix

Error:

Infinite yield possible on 'Players.Cyber_Designer:WaitForChild("Character")'

Code:

local player = game.Players.LocalPlayer
local character = player.Character or player:WaitForChild("Character")
local humanoid = character:WaitForChild("Humanoid")

local function checkSitting()
		if humanoid:GetState() == Enum.HumanoidStateType.Seated then
			print("sitting")
		else
			print("Not sitting")
		end
end

while wait(1) do
	checkSitting()
end

“Character” is a property, not an Instance; use player.CharacterAdded:Wait() instead.

Server Side:

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character.Humanoid
		
		while task.wait(1) do
			if Humanoid.Sit then
				print("Sitting")
			else
				print("Not Sitting")
			end
		end
	end)
end)

Client Side:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character.Humanoid

function CheckStatus()
	if Humanoid.Sit then
		print("Sitting")
	else
		print("Not Sitting")
	end
end

while task.wait(1) do
	CheckStatus()
end

Delete "character:WaitForChild(“Humanoid”) XD

The main problem here is that you try to fetch the character through WaitForChild instead of player.CharacterAdded:Wait(). Basically player.Character is a property, which is a pointer to the associated player character that exists under workspace(not under the player instance). But WaitForChild is looking specifically for instances within the game tree, not properties, and since no instance named "Character" exists under the player, it causes an infinite yield.

Here’s an event-based approach that should be working:

--Put script inside game.StarterPlayer.StarterCharacterScripts
local character = script.Parent 
local humanoid = character:FindFirstChildWhichIsA("Humanoid")

local oldSeat = humanoid.SeatPart
humanoid.Seated:Connect(function(active, seat)
	if active then
		oldSeat = seat 
		print(character, "Seated in", seat)
	else
		print(character, "Stopped Sitting in", oldSeat)
	end
end)

Scripts under game.StarterPlayer.StarterCharacterScripts load under the player character on each character load/respawn, that’s why script.Parent is equal to character in the above code.

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