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 ScreenGui
s 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
01 | local model = script.Parent |
02 | local connect 1 = model:WaitForChild( "JoinBlock1" ) |
03 | local connect 2 = model:WaitForChild( "JoinBlock2" ) |
04 | local full 1 = connect 1 :WaitForChild( "Full" ) |
05 | local full 2 = connect 2 :WaitForChild( "Full" ) |
07 | local ss = game:GetService( "ServerStorage" ) |
08 | local start 1 = ss:WaitForChild( "Start1" ) |
09 | local start 2 = ss:WaitForChild( "Start2" ) |
14 | local function CheckConnection() |
15 | if player 1 and player 2 and player 1 ~ = player 2 and full 1. Value = = true and full 2. Value = = true then |
16 | local pgui 1 = player 1 :WaitForChild( "PlayerGui" ) |
17 | local pgui 2 = player 2 :WaitForChild( "PlayerGui" ) |
19 | if not pgui 1 :FindFirstChild( "Start1" ) then |
20 | local clone 1 = start 1 :Clone() |
24 | if not pgui 2 :FindFirstChild( "Start2" ) then |
25 | local clone 2 = start 2 :Clone() |
31 | connect 1. Touched:Connect( function (hit) |
32 | player 1 = game:GetService( "Players" ):GetPlayerFromCharacter(hit.parent) |
36 | connect 2. Touched:Connect( function (hit) |
37 | player 2 = game:GetService( "Players" ):GetPlayerFromCharacter(hit.parent) |
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)