Why Isn't My Apartment Lock Script Working?

I Have A Apartment Model That Has A Gui That Asks The Player To Purchase An Apartment If The Player Says Yes Then It Will Teleport Them To An Available Apartment And I Have A Gui That Appears For The Player When They Enter The Apartment And It Gives Them The Option To Lock/Unlock Their Apartment The Lock Script Works Fine But Only For The First Apartment For Example If Their Is Only Two Players In The Game And They Both Buy An Apartment Then The Lock Script Will Only Work For The First Apartment And Will Not Work For The Second Apartment Here Are The Scripts:

This Is The Script That Locks The Apartment (Server Script)

--// Lock Apartment \\--
event.OnServerEvent:Connect(function(player)
	local leaderstats = player:WaitForChild('leaderstats')
	local OwnsHome = leaderstats:WaitForChild('OwnsHome')
	local HomeName = OwnsHome:WaitForChild('HomeName')
	if model1.IsOwned.Value == true then
		if OwnsHome.Value == true then
			if HomeName.Value == 1 then
				if model1:WaitForChild('IsLocked').Value == false then
					model1:WaitForChild('IsLocked').Value = true
					event2:FireClient(player, 'true')
				elseif model1:WaitForChild('IsLocked').Value == true then
					model1:WaitForChild('IsLocked').Value = false
					event2:FireClient(player, 'false')
				end
			end
		end
	else
		if model2.IsOwned.Value == true then
			if OwnsHome.Value == true then
				if HomeName.Value == 2 then
					if model2:WaitForChild('IsLocked').Value == false then
						model2:WaitForChild('IsLocked').Value = true
						event2:FireClient(player, 'true')
					elseif model2:WaitForChild('IsLocked').Value == true then
						model2:WaitForChild('IsLocked').Value = false
						event2:FireClient(player, 'false')
					end
				end
			end
		else
			if model3.IsOwned.Value == true then
				if OwnsHome.Value == true then
					if HomeName.Value == 3 then
						if model3:WaitForChild('IsLocked').Value == false then
							model3:WaitForChild('IsLocked').Value = true
							event2:FireClient(player, 'true')
						elseif model3:WaitForChild('IsLocked').Value == true then
							model3:WaitForChild('IsLocked').Value = false
							event2:FireClient(player, 'false')
						end
					end
				end
			else
				if model4.IsOwned.Value == true then
					if OwnsHome.Value == true then
						if HomeName.Value == 4 then
							if model4:WaitForChild('IsLocked').Value == false then
								model4:WaitForChild('IsLocked').Value = true
								event2:FireClient(player, 'true')
							elseif model4:WaitForChild('IsLocked').Value == true then
								model4:WaitForChild('IsLocked').Value = false
								event2:FireClient(player, 'false')
							end
						end
					end
				else
					if model5.IsOwned.Value == true then
						if OwnsHome.Value == true then
							if HomeName.Value == 5 then
								if model5:WaitForChild('IsLocked').Value == false then
									model5:WaitForChild('IsLocked').Value = true
									event2:FireClient(player, 'true')
								elseif model5:WaitForChild('IsLocked').Value == true then
									model5:WaitForChild('IsLocked').Value = false
									event2:FireClient(player, 'false')
								end	
							end
						end
					else
						if model6.IsOwned.Value == true then
							if OwnsHome.Value == true then
								if HomeName.Value == 6 then
									if model6:WaitForChild('IsLocked').Value == false then
										model6:WaitForChild('IsLocked').Value = true
										event2:FireClient(player, 'true')
									elseif model6:WaitForChild('IsLocked').Value == true then
										model6:WaitForChild('IsLocked').Value = false
										event2:FireClient(player, 'false')
									end	
								end
							end
						else
							if model7.IsOwned.Value == true then
								if OwnsHome.Value == true then
									if HomeName.Value == 7 then
										if model7:WaitForChild('IsLocked').Value == false then
											model7:WaitForChild('IsLocked').Value = true
											event2:FireClient(player, 'true')
										elseif model7:WaitForChild('IsLocked').Value == true then
											model7:WaitForChild('IsLocked').Value = false
											event2:FireClient(player, 'false')
										end	
									end
								end
							else
								if model8.IsOwned.Value == true then
									if OwnsHome.Value == true then
										if HomeName.Value == 8 then
											if model8:WaitForChild('IsLocked').Value == false then
												model8:WaitForChild('IsLocked').Value = true
												event2:FireClient(player, 'true')
											elseif model8:WaitForChild('IsLocked').Value == true then
												model8:WaitForChild('IsLocked').Value = false
												event2:FireClient(player, 'false')
											end
										end
									end
								else
									if model9.IsOwned.Value == true then
										if OwnsHome.Value == true then
											if HomeName.Value == 9 then
												if model9:WaitForChild('IsLocked').Value == false then
													model9:WaitForChild('IsLocked').Value = true
													event2:FireClient(player, 'false')
												elseif model9:WaitForChild('IsLocked').Value == true then
													model9:WaitForChild('IsLocked').Value = false
													event2:FireClient(player, 'false')
												end	
											end
										end
									else
										if model10.IsOwned.Value == true then
											if OwnsHome.Value == true then
												if HomeName.Value == 10 then
													if model10:WaitForChild('IsLocked').Value == false then
														model10:WaitForChild('IsLocked').Value = true
														event2:FireClient(player, 'true')
													elseif model10:WaitForChild('IsLocked').Value == true then
														model10:WaitForChild('IsLocked').Value = false
														event2:FireClient(player, 'false')
													end	
												end
											end
										else
											if model11.IsOwned.Value == true then
												if OwnsHome.Value == true then
													if HomeName.Value == 11 then
														if model11:WaitForChild('IsLocked').Value == false then
															model11:WaitForChild('IsLocked').Value = true
															event2:FireClient(player, 'true')
														elseif model11:WaitForChild('IsLocked').Value == true then
															model11:WaitForChild('IsLocked').Value = false
															event2:FireClient(player, 'false')
														end	
													end
												end
												if model12.IsOwned.Value == true then
													if OwnsHome.Value == true then
														if HomeName.Value == 12 then
															if model12:WaitForChild('IsLocked').Value == false then
																model12:WaitForChild('IsLocked').Value = true
																event2:FireClient(player, 'true')
															elseif model12:WaitForChild('IsLocked').Value == true then
																model12:WaitForChild('IsLocked').Value = false
																event2:FireClient(player, 'false')
															end	
														end
													end
												end
											end
										end
									end
								end
							end
						end
					end
				end
			end
		end
	end
end)

This Is The Script That Changes The Gui Text (Local Script)

local function work12()
	if Islocked12.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif Islocked12.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work11()
	if IsLocked11.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked11.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work10()
	if IsLocked10.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked10.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work9()
	if IsLocked9.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked9.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work8()
	if IsLocked8.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked8.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work7()
	if IsLocked7.Value == true then
		text.TextColor3 = Color3.new(0,1,0)
		text.Text = 'Status: Locked'
	elseif IsLocked7.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = ' Status: Unlocked'
	end
end

local function work6()
	if IsLocked6.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked6.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work5()
	if IsLocked5.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked5.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work4()
	if IsLocked4.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked4.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work3()
	if IsLocked3.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked3.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work2()
	if IsLocked2.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked2.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

local function work1()
	if IsLocked.Value == true then
		text.TextColor3 = Color3.new(0, 1, 0)
		text.Text = 'Status: Locked'
	elseif IsLocked.Value == false then
		text.TextColor3 = Color3.new(1, 0, 0)
		text.Text = 'Status: Unlocked'
	end
end

Islocked12.Changed:Connect(work12)
IsLocked11.Changed:Connect(work11)
IsLocked10.Changed:Connect(work10)
IsLocked9.Changed:Connect(work9)
IsLocked8.Changed:Connect(work8)
IsLocked7.Changed:Connect(work7)
IsLocked6.Changed:Connect(work6)
IsLocked5.Changed:Connect(work5)
IsLocked4.Changed:Connect(work4)
IsLocked3.Changed:Connect(work3)
IsLocked2.Changed:Connect(work2)
IsLocked.Changed:Connect(work1)

Sorry If This Is Confusing, Thanks

1 Like

I assume that the ā€œIsLockedā€ values are all stored in a folder? If so then the LocalScript could be formatted better, like this for example:

local lockedValuesFolder = nil
	
for i, isLocked in lockedValuesFolder:GetChildren() do
	
	isLocked.Changed:Connect(function(value)
		
		if value == true then
			
			text.TextColor3 = Color3.new(0, 1, 0)
			text.Text = 'Status: Locked'
			
		else
			
			text.TextColor3 = Color3.new(1, 0, 0)
			text.Text = 'Status: Unlocked'
			
		end
		
	end)
	
end

This does assume that the text variable is the same for all of the locked apartments, which I think might be your problem in this case.

1 Like

I Keep The ā€˜IsLocked’ Value Inside Each Apartment Model, Thanks

1 Like