Checking when player is sitting and toggling a gui and then remoteevent

So my game needs a feature where a gui is toggled when the player is sitting. Depending on what the player is sitting on, the gui will be different. One of the gui will allow the player to select certain test taking challenge IF the player meets certain stat requirements. When they select a challenge in the gui, the client will trigger a remote event for the server to do several things. If the player presses spacebar, they will get a prompt to abandon the challenge.

Right now I have this in a local script

local RunService = game:GetService("RunService")
local LocalPlayer = game:GetService("Players").LocalPlayer

local Character = LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Sitting = false
RunService.Heartbeat:Connect(function(delta)
	if Humanoid.Sit and (not Sitting) then
		print("Sat down, toggling gui")
		Sitting = true
	elseif not(Humanoid.Sit) and Sitting then
		print("got up, toggling gui")
		Sitting = false
	end
end)

Is this the best way to check if the player is sitting and toggle a gui? I will be cloning elements from replicated storage as each gui will be different. I’m worried about having the player sit down and then the gui not toggling properly, causing the player to be stuck.

You can either do this or use While true do.
If you want to do that.

Or in each seat you can do.

Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Seat.Occupant ~= nil then
	
	end
end)

You could use :GetPropertyChangedSignal instead:

local LocalPlayer = game:GetService("Players").LocalPlayer

local Character = LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")

local Sitting = false
Humanoid:GetPropertyChangedSignal("Sit"):Connect(function()
	if not Sitting then
		print("Sat down, toggling gui")
		Sitting = true
	elseif Sitting then
		print("got up, toggling gui")
		Sitting = false
	end
end)

Dang when i tried using that it didn’t work but you did it correctly, thanks

1 Like

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