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

Accessing PlayerGui On respawn?

Asked by 8 years ago

function onRespawn(character) local PlayerHere = character.Character.PlayerGui.PlayerHere -- How can i access a Player's Gui when they have respawned? PlayerHere.Value = false end local function onPlayerEntered(newPlayer) newPlayer.CharacterAdded:connect(onRespawn) end game.Players.PlayerAdded:connect(onPlayerEntered)

This is what i got so far. All help appreciated. Thanks :)

1 answer

Log in to vote
1
Answered by 8 years ago

You've almost done this right, but you're not looking in the right place for the PlayerGui. A PlayerGui exists directly in the player and not the character as Wizzy had stated previously.

NOTE: If you want my final script that has been shortened considerably, skip straight to the bottom. Otherwise, read on.

Now, to actually fix this, you need to direct your character variable to the actual player object in Players. How do we do this?

METHOD 1:

Our first method is to use GetPlayerFromCharacter, which is a function in the Players service that essentially "does what it says on the tin": It gets the player from their character. To use it in your function, you could do this and create a player variable:

local player = game.Players:GetPlayerFromCharacter(character)

What if the character is not associated with a player, what do we do then? Well, we need to add an if statement to check if the player is not nil, which is what is returned if GetPlayerFromCharacter can't find the player from the character.

if player then --Essentially the shorter version of "if player ~= nil then"

Your final function should be this for the first method:

function onRespawn(character)
    local player = game.Players:GetPlayerFromCharacter(character)
    if player then
        local PlayerHere = player.PlayerGui:WaitForChild("PlayerHere") --Waits for PlayerHere to become available.
        PlayerHere.Value = false 
    end
end 

METHOD 2:

Our second method is not to modify the entire onRespawn function, but to change both functions so that the player is the argument in this case.

Firstly, you want to have the onRespawn function set out like so:

function onRespawn(player)
    local PlayerHere = player.PlayerGui:WaitForChild("PlayerHere") --Waits for PlayerHere to become available.
    PlayerHere.Value = false 
end 

Now that the onRespawn function is set up properly, we can now edit the local function at the bottom to make sure the player is the argument we want.

So, instead of directly connecting the CharacterAdded event to the onRespawn function, we're going to wrap a function in the connection and then call the onRespawn function from there. Wrapping a function in a connection line is called an anonymous function.

local function onPlayerEntered(newPlayer)
    newPlayer.CharacterAdded:connect(function() --Leave the parenthesis for the end of the function to fully wrap it in the event connection.
        onRespawn(newPlayer) --See how we call the function and use newPlayer instead of directly connecting the event to the function?
    end) --Having an end with a parenthesis to fully wrap the function in the connection.
end 

game.Players.PlayerAdded:connect(onPlayerEntered)

Altogether, if you followed the second method to a tee, you should get this:

function onRespawn(player)
    local PlayerHere = player.PlayerGui:WaitForChild("PlayerHere") --Waits for PlayerHere to become available.
    PlayerHere.Value = false 
end 

local function onPlayerEntered(newPlayer)
    newPlayer.CharacterAdded:connect(function() --Leave the parenthesis for the end of the function to fully wrap it in the event connection.
        onRespawn(newPlayer) --See how we call the function and use newPlayer instead of directly connecting the event to the function?
    end) --Having an end with a parenthesis to fully wrap the function in the connection.
end 

game.Players.PlayerAdded:connect(onPlayerEntered)

Shortening your code:

There is an easier way of doing all of this. Instead of making a separate function outside the CharacterAdded connection, we could just wrap the code for the entire function in the connection line.

local function onPlayerEntered(newPlayer)
    newPlayer.CharacterAdded:connect(function() --Leave the parenthesis for the end of the function to fully wrap it in the event connection.
        local PlayerHere = newPlayer.PlayerGui:WaitForChild("PlayerHere") --Use the newPlayer variable here since it's available for us to use, unlike previously.
        PlayerHere.Value = false
    end) --Having an end with a parenthesis to fully wrap the function in the connection.
end 

game.Players.PlayerAdded:connect(onPlayerEntered)

You can also skip connecting the PlayerAdded event to the onPlayerEntered function and make it an anonymous function.

game.Players.PlayerAdded:connect(function(newPlayer) --Anonymous function for the PlayerAdded event.
    newPlayer.CharacterAdded:connect(function() --Leave the parenthesis for the end of the function to fully wrap it in the event connection.
        local PlayerHere = newPlayer.PlayerGui:WaitForChild("PlayerHere") --Use the newPlayer variable here since it's available for us to use, unlike previously.
        PlayerHere.Value = false
    end) --Having an end with a parenthesis to fully wrap the function in the CharacterAdded connection.
end) --Having an end with a parenthesis to fully wrap the function in the PlayerAdded connection.

I hope my answer has helped you. If it did, be sure to accept it.

0
Haha, thanks for taking the time and effort to help answer this question, thanks again :) Bubbles5610 217 — 8y
0
No problem. Spongocardo 1991 — 8y
Ad

Answer this question