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")
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")
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()
onRespawn(newPlayer)
end)
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")
PlayerHere.Value = false
end
local function onPlayerEntered(newPlayer)
newPlayer.CharacterAdded:connect(function()
onRespawn(newPlayer)
end)
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()
local PlayerHere = newPlayer.PlayerGui:WaitForChild("PlayerHere")
PlayerHere.Value = false
end)
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)
newPlayer.CharacterAdded:connect(function()
local PlayerHere = newPlayer.PlayerGui:WaitForChild("PlayerHere")
PlayerHere.Value = false
end)
end)
I hope my answer has helped you. If it did, be sure to accept it.
2 comments
Haha, thanks for taking the time and effort to help answer this question, thanks again :)
No problem.