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

How would you find the two players who hit a block and make the GUI popup?

Asked by 5 years ago

I'm trying to make it so when both values are true, the function will find the two players who hit the block and insert the GUI in their PlayerGui. I think the issue might be with the function.

local player1 = script.Parent:FindFirstChild("JoinBlock1").Touched:Connect(function(hit1)
     game.Players:GetPlayerFromCharacter(hit1.parent)
end)

local player2 = script.Parent:FindFirstChild("JoinBlock2").Touched:Connect(function(hit2)
     game.Players:GetPlayerFromCharacter(hit2.parent)
end)

if script.Parent:FindFirstChild("JoinBlock1").Full.Value == true and
    script.Parent:FindFirstChild("JoinBlock2").Full.Value == true then
        game.SeverStorage.Start1.Parent = player1.PlayerGui
        game.SeverStorage.Start2.Parent = player2.PlayerGui
end


1 answer

Log in to vote
0
Answered by 5 years ago

Improvements

Use local variables for instances which are used multiple times throughout your code

Use :GetService() to retrieve the ServerStorage and Players services

Use :WaitForChild() to make sure an instance exists before using it (an infinite yield will prevent code from running if it doesn't) as otherwise you are potentially referencing nil values

Issues

The cloning should take place after each of the blocks are touched (I've used a local function so as to not rewrite redundant code). The way you have it now, the code will only run when the script is first created.

Use :Clone() in case the ScreenGuis are to be redistributed to multiple players (your current code would only allow this entire function to run once successfully)

Based on your question, also check that player1 and player2 are not the same player before performing a cloning.

Revised Server Script

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

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)

I also added a check to make sure the player did not already have the gui that is meant to be given to them in their PlayerGui (to preventing the cloning unnecessarily)

0
Thanks a lot! XxOPGUYxX1234567 221 — 5y
Ad

Answer this question