Problem in a touched script

So I’ve got a problem in a script which is supposed to open a UI once a brick with a specific name is touched, yet it doesn’t open a UI.
Here is the script:


local GUI = game.ReplicatedStorage.HouseSystem.Trailer
local bool = false
for _, v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") then
v.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") and v.Name == "TrailerTouchedPart"  and bool == false then 
bool = true
print("works")
-- code starts here
  local plr = game.Players[hit.Parent.Name]
if plr:FindFirstChild("PlayerGui") and not plr.PlayerGui:FindFirstChild(GUI.Name) and bool == false and plr.HouseData:WaitForChild("TrailerOwnedSlot").Value ~= v.Parent.Parent.Parent.Name then
bool = true   
GUI:Clone().Parent = plr.PlayerGui
plr.PlayerGui.Trailer.PropertyBought.Value = v.Parent.Parent.Parent
wait(5)
bool = false 
elseif plr.HouseData:WaitForChild("TrailerOwnedSlot").Value == v.Parent.Parent.Parent.Name and plr.HouseData:WaitForChild("HasClaimed").Value == false and bool == false then
	bool = true  
	game.ReplicatedStorage.HouseSystem.ClaimProperty:Clone().Parent = plr.PlayerGui
  	wait(1)
 	plr.PlayerGui.ClaimProperty.Property.Value = v.Parent.Parent.Parent
	wait(5)
	bool = false
	elseif plr.HouseData:WaitForChild("HouseNumber").Value == v.Parent.Parent.Parent.HouseNumber.Value then
		v.CanCollide = true
		wait(1)
		v.CanCollide = false
 end
-- ends here
wait(3)
bool = false
end
end)
end
end

Does the print statement used for debugging, “works” print?

Yeah, it does actually, that’s why I am confused.

You should use the GetPlayerFromCharacter() function for this.
Also, check if the player actually exists before doing anything else, else the function might error and the Script might stop working.

local plr = game.Players:GetPlayerFromCharacter(hit.Parent)

if not plr then
    return
end
--code continues from here

As for your problem, take a look at these 2 lines.

Do you see the mistake? You are checking if the bool variable is false after setting it to true.