How do I know if my Bed is occupied?

  1. What do you want to achieve? The title says it.

  2. What is the issue? Can’t do it, tried adding functions but doesn’t work.

  3. What solutions have you tried so far? I looked for solutions but didn’t find any.

The script:

local pp = script.Parent:WaitForChild("ProximityPrompt")
local d = false
local a = script.Parent:WaitForChild("Animation")

pp.Triggered:Connect(function(p)
	local c = p.Character or p.CharacterAdded:Wait()
	local v = c:WaitForChild("isHiding")
	local h = c:WaitForChild("Humanoid")
	local r = c:WaitForChild("HumanoidRootPart")
	local t = h:LoadAnimation(a)

	if not d then
		d = true
		v.Value = true
		pp.ActionText = "Go Out"
		t:Play()
		r.Position = script.Parent.Parent.Positions.Pos.Position
		r.Anchored = true
		r.CanCollide = false
		r.Orientation = script.Parent.Parent.Positions.Pos.Orientation
		h.AutoRotate = false
		script.Parent:WaitForChild("isOccupied").Value = true
	else
		v.Value = false
		pp.ActionText = "Hide"
		for i, v in pairs(h:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		r.Position = script.Parent.Parent.Positions.OutPos.Position
		r.Anchored = false
		r.CanCollide = true
		h.AutoRotate = true
		script.Parent:WaitForChild("isOccupied").Value = false
		d = false
	end
end)

The Explorers Tab:
image

What exactly isn’t working?
PS: I don’t think you need to use WaitForChild at all in this script, just using a . should work.

If you want you could use a seat and detect if there is an occupant, not necessarily a solution however.

Are there any errors coming into your output? When you trigger the proximity prompt, does your ‘isOccupied’ value changing?

Yes, the exact thing i really need here is to know if it’s occupied or not, so if someone triggers the prompt, and someone’s using the bed, it just returns end.

Try just setting an occupied boolen attribute when they player is in the bed something like this below i also commented so you can learn what each change does

local pp = script.Parent:WaitForChild("ProximityPrompt")
--local d = false  -- if this is the occupied debounce don't think you need it here
local Debounce   -- use this as local debounce to give alittle time between clicks
local a = script.Parent:WaitForChild("Animation")

pp.Triggered:Connect(function(p)
	if Debounce then return end  -- already clicked
	Debounce = true
	-- using delay function like this guarantees debouce reset even with error
	delay(1, function()  -- delay 1 second then remove debounce so it can be clicked again 
		Debounce = nil
	end)
	local c = p.Character or p.CharacterAdded:Wait()
	local v = c:WaitForChild("isHiding")
	local h = c:WaitForChild("Humanoid")
	local r = c:WaitForChild("HumanoidRootPart")
	local t = h:LoadAnimation(a)

	if not script.Parent:GetAttribute('Occupied') then  -- checks if its not occupied.. if not set to occupied else set unoccupied
		script.Parent:SetAttribute('Occupied', true)   -- sets the attribute as boolen to true
		v.Value = true
		pp.ActionText = "Go Out"
		t:Play()
		r.Position = script.Parent.Parent.Positions.Pos.Position
		r.Anchored = true
		r.CanCollide = false
		r.Orientation = script.Parent.Parent.Positions.Pos.Orientation
		h.AutoRotate = false
		--script.Parent:WaitForChild("isOccupied").Value = true -- don't think you need this just use attributes
	else
		v.Value = false
		pp.ActionText = "Hide"
		for i, v in pairs(h:GetPlayingAnimationTracks()) do
			v:Stop()
		end
		r.Position = script.Parent.Parent.Positions.OutPos.Position
		r.Anchored = false
		r.CanCollide = true
		h.AutoRotate = true
		script.Parent:SetAttribute('Occupied', nil)  -- removes the attribute so it shows not occupied anymore
		--script.Parent:WaitForChild("isOccupied").Value = false  -- don't think you need this just use attributes
	end
end)
1 Like

Does this work when a player is using the bed and another player triggers it and it doesnt work anymore unless the player using the bed goes out?

you would need to setup a server script to set the attributes server side else if this is in local only the client can see it

the script is just a server script