Fire a Client Remote Event on Humanoid Seat

Hello devs, I’m looking for a bit of help! I am trying to fire a remote event on the client when the player sits on a normal seat. The problem is that I don’t receive any error in the output and the actions that should be performed when the event is fired do not occur.

Here are my scripts:

Script inside a Proximity Prompt of a Seat in Workspace

local proximity = script.Parent
local bedSeat = proximity.Parent

local players = game:GetService("Players")

local rEvents = game.ReplicatedStorage:WaitForChild("TaskREvents")

--disable proximity if someones using the seat
bedSeat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if bedSeat.Occupant then
		proximity.Enabled = false
	else
		proximity.Enabled = true
	end
end)

local function onSeated(isSeated, seat)
	if isSeated then
		if seat:GetAttribute("typeOfSeat") then
			local currentSeatType = seat:GetAttribute("typeOfSeat")
			if currentSeatType == "bed" then
				print("player is seated on a bed")
				local plyr = players:GetPlayerFromCharacter(seat.Occupant.Parent)
				rEvents.CompleteSleep:FireClient(plyr)
			end
		end
	else
		print("player is not seated")
	end
end

local function onProximityPrompted(player)
	bedSeat:Sit(player.Character.Humanoid)
	player.Character.Humanoid.Seated:Connect(onSeated)
end

--main seat function
proximity.Triggered:Connect(onProximityPrompted)

LocalScript inside StarterGUI

game.ReplicatedStorage.TaskREvents.CompleteSleep.OnClientEvent:Connect(function()
	print("hey")
end)

Hope I can get some help :wink: Thanks!

3 Likes

Does

print("player is seated on a bed")

output anything?


Are you certain that onSeated() is actually firing?
If not, try moving

player.Character.Humanoid.Seated:Connect(onSeated)

to before

bedSeat:Sit(player.Character.Humanoid)

(BTW, there is a potential memory leak here in your current code, due to the connection event being made each time that the proximity prompt is triggered. You may be able to use :Once instead of :Connect)

1 Like

Hello! Thanks for your answer.

Yes, print("player is seated on a bed") output the message successfully so the onSeated() function is actually firing.

I don’t have any error on the output. The onSeated() function works because it actually print the messages (player is seated on a bed and player is not seated), but the Remote Event doesn’t fire because I don’t receive the message from the LocalScript on StarterGui.

1 Like