Dear Reader,
I am trying to make a script that changes the Parent of my GUI from the PlayerGUI back to the StarterGUI. When I attempt to do so, I got an error.
Please see the following code below.
1 | function PlayerTouched(Part) |
2 | game.Players.LocalPlayer.PlayerGUI.BuyALC.Parent = game.StarterGUI |
3 | end |
4 | game.Workspace.ALC.RemovePurchase.Touched:connect(PlayerTouched) |
I don't know how to fix this and I need help in doing so.
Thank you.
You wrote PlayerGui wrong, also with StarterGui. Remember to just have the words with one caps if there is 2 words connected in a word.
Try using the WaitForChild thing, here
1 | function PlayerTouched(Part) |
2 | game.Players.LocalPlayer:WaitForChild( "PlayerGui" ).BuyALC.Parent = game.StarterGUI |
3 | end |
4 | game.Workspace.ALC.RemovePurchase.Touched:connect(PlayerTouched) |
Also make sure this GUI script is a LocalScript cause you're using LocalPlayer.
I cannot really understand what you are trying to do, however you should be accessing the player through the :GetPlayerFromCharacter()
function to access a player through a server script which must be used since you are using a .Touched event.
Using game.Players.LocalPlayer
WILL NOT WORK in a Server Script, yet if you will put it in a Local Script, the script must always be inside of the client and not on the server, hence, if you will put a local script in a part which is in the workspace, this script will only work while you are in Studio but not in game.
This is how to use the :GetPlayerFromCharacter()
function
1 | function PlayerTouched(Part) |
2 | if Part.Parent:FindFirstChild( "Humanoid" ) then |
3 | local character = Part.Parent |
4 | local player = game.Players:GetPlayerFromCharacter(character) |
5 | end |
6 | end |
7 | workspace.ALC.Touched:connect(PlayerTouched) |