Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does my GUI that opens when touched only works once?

So I have a shop GUI where if the player touches the part, a GUI will pop up and when the GUI is closed and I touch the button again, the GUI doesnt show up now, what's going on? You can also provide an explanation because I have no idea what filtering enabled is if that is the solution. Thanks!

EDIT (SOLUTION) : DONT USE LOCAL SCRIPT, USE A NORMAL SCRIPT

Part script :

button = script.Parent

function onTouch(part)
	if part.Parent:FindFirstChild("Humanoid") ~= nil then
		local player = Game.Players:GetPlayerFromCharacter(part.Parent)
		if player ~= nil then
			local GUI = Game.ReplicatedStorage:FindFirstChild(script.Parent.ShopGUI.Value)
			local AlreadyHaveGUI = player.PlayerGui:FindFirstChild(script.Parent.ShopGUI.Value)
			if AlreadyHaveGUI == nil then
			GUI:Clone().Parent = player.PlayerGui
			else print("Already have GUI!")
			end
		end
	else print("Humanoid not found!")
	end
end

button.Touched:connect(onTouch)

Close script :

function close()
game.Players.LocalPlayer.PlayerGui.ArmorShopGUI2:Remove()
end

script.Parent.MouseButton1Click:connect(close)

1 answer

✓ Accepted answer

Edit: Whoops I misread the code. Maybe going about checking for the GUI needs to be done differently.

button = script.Parent

function onTouch(part)
    if part.Parent:FindFirstChild("Humanoid") ~= nil then
        local player = game.Players:GetPlayerFromCharacter(part.Parent)
        if player ~= nil then
	    --// Make the players GUI a variable
	    local playerGui = player.PlayerGui

            local GUI = Game.ReplicatedStorage:FindFirstChild(script.Parent.ShopGUI.Value)

            --// Instead of checking with nil use FindFirstChild
            if playerGui:FindFirstChild(script.Parent.ShopGUI.Value)  then
            	print("Already have GUI!")
            else
			local guiClone = GUI:Clone()
			guiClone.Parent = playerGui 
            end
        end
    else 
print("Humanoid not found!")
    end
end

button.Touched:connect(onTouch)

3 comments

0

I tried using the code but it still only works once, where if I try again it will always print "Already have GUI!", any ideas? Thanks for your answer.

0

Oh I know what is wrong, I was using a LocalScript instead of a normal Script, thanks anyways!

0

Ahh lol yeah that'll do it. No problem.

This discussion is preserved as it was. New answers, comments, and votes are closed.