This is to make the gui visible or invisible for a tycoon game, Im trying to add a brick that upon touch the Visible Box in The gui property becomes True. I can not figure out what I am missing. Also I may be wrong but I do not believe I will need debounce right? Please help me!
local p = script.Parrent local v = game.Players:findFirstChild("PlayerName").ScreenGui.TeleportGui.TxtBox.Visible Function onTouch(part) V = true end p.Touched:connect(onTouch)
local p = script.Parrent -- "Parrent" should be "Parent" local v = game.Players:findFirstChild("PlayerName").ScreenGui.TeleportGui.TxtBox.Visible -- "TxtBox" should (maybe, unless if you renamed it) to "TextBox"; another possible error is the "'PlayerName'" bit; also, you didn't refer to the "player's" PlayerGui Function onTouch(part) --"Function" should be "function" V = true -- "V" should be "v" for consistency with your variables end p.Touched:connect(onTouch)
This script does not need a debounce. Debouncing is a concept in which only one set of executions can run per session. That way your executions doesn't look like it is glitching.
For example (a poor example at that), to turn on a computer, the process it takes to do so is about 20 seconds. If you click the power button once, it will start the process. If you click it again, the computer won't respond. And it will not respond until the 20 seconds is up.
But what you do need is to do some heavy debugging with this.
local p = script.Parent local v = game.Players:FindFirstChild("PlayerName").PlayerGui.ScreenGui.TeleportGui.TxtBox function onTouch(part) v.Visible = true end p.Touched:connect(onTouch)
That looks better. Kind of.
The only problem you're going to run into is line 2; where the script tries to find WHICH player its referring to, in order to change the property you so desire.
So let's change one of your variables and your function.
-- Remove line 2! function onTouch(part) local HumanoidIsFound = part.Parent:FindFirstChild("Humanoid") local PlayerIsFound = game.Players:GetPlayerFromCharacter(part.Parent) if HumanoidIsFound and PlayerIsFound then PlayerIsFound.PlayerGui.ScreenGui.TeleportGui.TxtBox.Visible = true end end
:FindFirstChild()
& :GetPlayerFromCharacter()
is awesome. These are what I like to call as verifiers (not a word; Chrome can verify). They verify whether the object you're looking for exists in the specified location.
:FindFirstChild()
returns the object you're looking for, or nil.
:GetPlayerFromCharacter()
returns the Player instance you're looking for from the character model you've given, or nil.
What line 6 (in the last code block) is saying is if...
A Humanoid exists
A player exists in the Player instance from the character you've given in the method
...then the player's (that you're referring to) TxtBox
will be visible!
Any questions/comments/skepticism? Check back with me. I'm here to help.
There are multiple problems with your code;
Line 1
, You spelt Parent
wrong, you wrote it as Parrent
.
Line 2
, To me, that much code is NOT necessary, also, what if there isn't a Child named PlayerName
, and if there was, what if ScreenGui
was not existant? Lastly, why is it not in the Player's PlayerGui? Why not use the WaitForChild method to repeat waiting until the Child is existant?
Line 4
, You spelt function
incorrectly, you wrote it as Function
.
Line 5
, You are not setting the GUI
's Visible
property to true
, it is creating a new Global Variable V
, which is set to true
.
Now, as I have stated, there are only a few problems with your code, but, sadly, still breaks your code, however, this can all be fixed, by using the WaitForChild method, If statement, and rewriting some of your code. Now, let's fix your code;
local p = script.Parent; --Variable 'p' is now identifying 'script.Parent' [As you already know :P] local Debounce = false; --Let's use a Debounce; 'Debounce''s are used to prevent a function/chunk of code from firing multiple times on execution function onTouch(hit) --Here is your original function; I changed the Argument Variable to 'hit' to reflect on the chunk if not hit.Parent and Debounce then return false end --If not the BasePart type instance's Parent then [I have no idea why people use this o_e] it will return, and not let the chunk go through, or, if 'Debounce' is true, it will not allow the chunk to go through either Debounce = true --Will revert 'Debounce' to true, preventing the code from firing multiple times on execution if game.Players:GetPlayerFromCharacter(hit.Parent) then --The 'if' statement is checking to see if 'hit.Parent' is a Player, if so, it will run the next chunk; local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --Local Variable 'plr' is now identifying the 'Player' currently touching the Part if plr:FindFirstChild("PlayerGui") then --If the Child 'PlayerGui' is existant within the 'Player', or, within 'plr', then it will run the next chunk; if plr.PlayerGui:FindFirstChild("ScreenGui") and plr.PlayerGui.ScreenGui:FindFirstChild("TeleportGui") and plr.PlayerGui.ScreenGui.TeleportGui:FindFirstChild("TxtBox") then --If the Child 'ScreenGui' is existant within the Player's PlayerGui, 'TeleportGui' is existant within 'ScreenGui', and 'TxtBox' is existant within 'TeleportGui' then it will run the next chunk of code; plr.PlayerGui.ScreenGui.TeleportGui.TxtBox.Visible = true --Will set 'TxtBox''s 'Visible' property to true end --This ends the chunk of code for the 'if' statement end --This ends the chunk of code for the 'if' statement end --This ends the chunk of code for the 'if' statement wait(2) --Waits 2 seconds Debounce = false --Reverts 'Debounce' to false, allowing the user to use it again end --This ends the chunk of code for the function p.Touched:connect(onTouch) --The event will fire whenever 'p' [or the Script's Parent] is Touched
Hope this helped!
Locked by TheeDeathCaster, Aethex, and Redbullusa
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?