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

:Destroy() isn't working..?

Asked by 9 years ago

Its suppose to destroying the GUI when you leave the room, but it's not...why?

local player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(player)
    if player.PlayerGui.DJPanel.MusicScreen.Visible == true then
    player.PlayerGui.DJPanel:Destroy()
    if player.PlayerGui.DJPanel.EffectScreen.Visible == true then
        player.PlayerGui.DJPanel:Destroy()
        if player.PlayerGui.DJPanel.Frame.Visible == true then
            player.PlayerGui.DJPanel:Destroy()
        end
    end
end 
end)


0
What kind of a script, and where is the script located? parkderp1 105 — 9y
0
It's a local script, and its located in the part. UnleashedGamers 257 — 9y
0
Local scripts must be placed into a descendant of the Player. So put the script inside 'Starterpack' or 'StarterGui' DigitalVeer 1473 — 9y
0
Still didn't remove the gui. UnleashedGamers 257 — 9y
1
Local Guis must be put inside StarterGui drybones3363 30 — 9y

2 answers

Log in to vote
1
Answered by 9 years ago

Note: Giving us an error message (in Output window) makes it easier to figure out what's wrong. If you're testing online, press F9 for the Developer Console.

You have a local variable named "player" (line 1) and also an argument named "player" (line 3), which hides the local variable on line 1.

.Touched gives you a part, so you should probably make sure the part is part of the player.

If you Destroy DJPanel, you won't be able to destroy its children (but that'll be done automatically).

local player = game.Players.LocalPlayer

script.Parent.Touched:connect(function(part)
    if part.Parent ~= player.Character then return end --part not from player
    if player.PlayerGui.DJPanel.MusicScreen.Visible == true then
        player.PlayerGui.DJPanel:Destroy()
    end
end)

If you put the script in a Gui, be sure to change "script.Parent" to be the proper Part in the workspace.

Ad
Log in to vote
1
Answered by 9 years ago

The problem that you are having is quite easy to fix and is an honest mistake, everyone has made this mistake before. When a part is touched by a user, the body part that hits the object that was touched is a member of the player's character and is not within the player and is not the player. You are simply trying to destroy a Gui that is inside a body part and does not exist. You can easily fix this with the GetPlayerFromCharacter method.

In addition to this problem, you cannot have a localscript placed within a part that is in Workspace. I have fixed the script for you and this should work, if it does not, double-check your hierarchical structure. You are also attempting to destroy a single object 3 times, which will throw an error, I have also fixed that for you.

player = nil
panel = nil
script.Parent.Touched:connect(function(body)
    player = game.Players:GetPlayerFromCharacter(body.Parent)
    panel = player.PlayerGui.DJPanel
    if panel.MusicScreen.Visible == true or panel.EffectScreen.Visible == true or panel.Frame.Visible == true then
            panel:Destroy()
    end 
end)

Answer this question