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.

01local player1 = script.Parent:FindFirstChild("JoinBlock1").Touched:Connect(function(hit1)
02     game.Players:GetPlayerFromCharacter(hit1.parent)
03end)
04 
05local player2 = script.Parent:FindFirstChild("JoinBlock2").Touched:Connect(function(hit2)
06     game.Players:GetPlayerFromCharacter(hit2.parent)
07end)
08 
09if script.Parent:FindFirstChild("JoinBlock1").Full.Value == true and
10    script.Parent:FindFirstChild("JoinBlock2").Full.Value == true then
11        game.SeverStorage.Start1.Parent = player1.PlayerGui
12        game.SeverStorage.Start2.Parent = player2.PlayerGui
13end

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

01local model = script.Parent
02local connect1 = model:WaitForChild("JoinBlock1")
03local connect2 = model:WaitForChild("JoinBlock2")
04local full1 = connect1:WaitForChild("Full")
05local full2 = connect2:WaitForChild("Full")
06 
07local ss = game:GetService("ServerStorage")
08local start1 = ss:WaitForChild("Start1")
09local start2 = ss:WaitForChild("Start2")
10 
11local player1 = nil
12local player2 = nil
13 
14local function CheckConnection()
15    if player1 and player2 and player1 ~= player2 and full1.Value == true and full2.Value == true then
View all 39 lines...

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