I am trying to erase the extra images of a GUI but it doesn't seem to be working. I've had many trial and errors but it does not fit to my specifications. I want the 'Drop', in fact all of them, to become invisible and visible again. Nothing seems to be working. Here's the script:
local isOn = false g = game.Players.Player.PlayerGui.SnowGUI:GetChildren() print("1") function onTouched() if isOn == true then off() else on() end print("2") end function on() isOn = true game.Players.Player.PlayerGui.SnowGUI.GUI.Disabled = true game.Players.Player.PlayerGui.SnowGUI.Frame.BackgroundTransparency = 1 print("3") for i = 1, # g do if g[i].className == "Drop" then g[i].Visible = true end end end function off() isOn = false game.Players.Player.PlayerGui.SnowGUI.GUI.Disabled = false game.Players.Player.PlayerGui.SnowGUI.Frame.BackgroundTransparency = 0.9 print("4") for i = 1, # g do if g[i].className == "Drop" then g[i].Visible = false end end end print("5") script.Parent.Touched:connect(onTouched) on() `
Your problem is game.Players.Player
.
On Roblox, the dot (.
) signifies that you are looking for a child or property of whatever object the dot follows, with the name of whatever the dot precedes. If the child or property does not exist, you will get an error.
Which is exactly what's happening. The dot looks for children based on their Name, but there will never be a person named "Player" in your game. Attempting to use something that does not exist will result in an error.
A solution to this would be to use the GetPlayerFromCharacter
method. This method will take a character and find the player controlling it. Since Touched events have a parameter equal to the specific Part that did the touching, if an actual Player touched it, we can access their character by getting the Parent of the part that did the touching. Now we can use GetPlayerFromCharacter.
Here's an example of GetPlayerFromCharacter;
function whenTouched(hit) --make sure a real player touched it if game.Players:GetPlayerFromCharacter(hit.Parent) then --store it in a variable local plr = game.Players:GetPlayerFromCharacter(hit.Parent) --destroy all the guis. Use WaitForChild in case it has not loaded yet. plr:WaitForChild("PlayerGui"):ClearAllChildren() end end