Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why won't GUI destroy when one value or the other becomes false?

Asked by 4 years ago
Edited 4 years ago

So I have this script that makes it when two values are true meaning that two players are touching two separate blocks (Thanks to SerpentineKing), the function finds the two players hitting the block and adds a GUI into their PlayerGUI. I am now trying to make it so when one of the people aren't touching one of the two blocks anymore, meaning one of the values become false, the GUI is destroyed from both player's PlayerGUI, and then this script will work when both players touch the two blocks again.

local model = script.Parent
local connect1 = model:WaitForChild("JoinBlock1")
local connect2 = model:WaitForChild("JoinBlock2")
local full1 = connect1:WaitForChild("Full")
local full2 = connect2:WaitForChild("Full")

local ss = game:GetService("ServerStorage")
local start1 = ss:WaitForChild("Start1")
local start2 = ss:WaitForChild("Start2")

local player1 = nil
local player2 = nil

local function CheckConnection()
    if player1 and player2 and player1 ~= player2 and full1.Value == true and full2.Value == true then
        local pgui1 = player1:WaitForChild("PlayerGui")
        local pgui2 = player2:WaitForChild("PlayerGui")

        if not pgui1:FindFirstChild("Start1") then
            local clone1 = start1:Clone()
            clone1.Parent = pgui1
        end

        if not pgui2:FindFirstChild("Start2") then
            local clone2 = start2:Clone()
            clone2.Parent = pgui2
        end
    end
end

local function CheckConnectionEnded()
    if player1 and player2 and player1 ~= player2 and full1.Value == false or full2.Value == false then
        local pgui1 = player1:WaitForChild("PlayerGui")
        local pgui2 = player2:WaitForChild("PlayerGui")

        if pgui1:FindFirstChild("Start1") then
          start1:Destroy()
        end

        if pgui2:FindFirstChild("Start2") then
           start2:destroy()
        end
    end
end

connect1.Touched:Connect(function(hit)
    player1 = game:GetService("Players"):GetPlayerFromCharacter(hit.parent)
    CheckConnection()
end)

connect2.Touched:Connect(function(hit)
    player2 = game:GetService("Players"):GetPlayerFromCharacter(hit.parent)
    CheckConnection()
end)

connect1.TouchEnded:Connect(function(ended)
    player1 = game:GetService("Players"):GetPlayerFromCharacter(ended.parent)
    CheckConnectionEnded()
end)

connect2.TouchEnded:Connect(function(ended)
    player2 = game:GetService("Players"):GetPlayerFromCharacter(ended.parent)
    CheckConnectionEnded()
end)

Answer this question