Bed System Like Break In Story

Whats up guys, im trying to make a bed system like cracky4’s break in, right now im trying to work on when you sit down your name pops up on the billboard gui and if you want to take another bed your old bed unclaims its self and lastly when you get up people cant take your claimed bed. here is my current script nothing is working for me. It’s a script in the bed its self.

local claimedBeds = {} -- Table to keep track of claimed beds and their owners

-- Function to claim the bed
local function ClaimBed(player, claimedSeat)
	if not claimedBeds[claimedSeat] then
		claimedBeds[claimedSeat] = player

		local billboardGui = bed:FindFirstChild("BillboardGui")
		-- Display the BillboardGui with the player's name
		if billboardGui and billboardGui:IsA("BillboardGui") and player.Name then
			-- Check if the text has been set, if not, set the text and lock it
			if not billboardGui.TextLabel:GetAttribute("TextSet") then
				billboardGui.TextLabel.Text = player.Name
				billboardGui.TextLabel:SetAttribute("TextSet", true) -- Lock the text
			end
			billboardGui.Enabled = true
		end
	end
end

-- Event: When a player sits on the bed
local function OnOccupantChanged(seat)
	local occupant = seat.Occupant
	if occupant then
		local player = game.Players:GetPlayerFromCharacter(occupant.Parent)
		if player then
			local claimedPlayer = claimedBeds[seat]
			if claimedPlayer and player ~= claimedPlayer then
				-- If another player tries to sit on the claimed bed, don't allow it
				seat.CanTouch = false
				seat.Disabled = true
				seat.CanQuery = false
				seat.CanCollide = false
			end
		end
	end
end

-- Connect the claim bed function to the bed's BillboardGui
local billboardGui = bed:FindFirstChild("BillboardGui")
if billboardGui then
	billboardGui:GetPropertyChangedSignal("Enabled"):Connect(function()
		if billboardGui.Enabled == true then
			local player = claimedBeds[next(claimedBeds)] -- Get the first player who claimed this bed
			if player then
				local occupant = bed:GetChildren()[1].Occupant
				if occupant then
					local occupantPlayer = game.Players:GetPlayerFromCharacter(occupant.Parent)
					if not occupantPlayer or occupantPlayer ~= player then
						-- If the bed is occupied by another player or empty, disable it
						bed.CanQuery = false
						bed.CanCollide = false
						bed.CanTouch = false
						bed.Disabled = true
					else
						-- If the bed is occupied by the owner, enable the seat for the occupant
						OnOccupantChanged(bed:GetChildren()[1])
					end
				end
			end
		end
	end)
end

-- Event: When a player jumps off the bed
local function OnHumanoidSeated(active, seat)
	if not active and seat:IsDescendantOf(bed) then
		local occupant = seat.Occupant
		if occupant then
			local player = game.Players:GetPlayerFromCharacter(occupant.Parent)
			if player then
				local claimedPlayer = claimedBeds[seat]
				if claimedPlayer and player == claimedPlayer then
					-- If the owner jumps off the bed, destroy the seat for other players
					for _, otherSeat in ipairs(seatModel:GetChildren()) do
						if otherSeat:IsA("Seat") and otherSeat ~= seat then
							otherSeat:Destroy()
						end
					end
				else
					-- If another player sits on the claimed seat, destroy it for them
					seat:Destroy()
				end
			end
		end
	end
end

-- Connect the Humanoid.Seated event to detect when a player leaves the seat
for _, seat in ipairs(seatModel:GetChildren()) do
	if seat:IsA("Seat") then
		local humanoid = seat:FindFirstChildOfClass("Humanoid")
		if humanoid then
			humanoid.Seated:Connect(function(active)
				OnHumanoidSeated(active, seat)
			end)
		end
	end
end

-- Event: When a player leaves the seat
game.Players.PlayerRemoving:Connect(function(player)
	for claimedSeat, claimedPlayer in pairs(claimedBeds) do
		if claimedPlayer == player then
			-- Destroy the seat completely when the owner leaves
			local seat = claimedSeat
			seat:Destroy()
			claimedBeds[claimedSeat] = nil
			break
		end
	end
end)

Can you edit the post with the code formatted correctly so it is easier to read.

like this

alright does that work? btw to give u info when i sit on the seat nothing happens no output either.

i got it to work everyone it was such a simple script thanks!

1 Like

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