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

Help with deleting a gui by clicking on a part?

I want to make it to where if you click a part, the gui would go away/ destroyed. My current script doesn't work. Here it is:

function onClicked(playerWhoClicked)
	local guiName = game.LocalPlayer.PlayerGui.GameName -- Game name gui
	wait(0.1)
	guiName:Destroy()
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

The error says:

**LocalPlayer is not a valid member of DataModel

Help?

3 answers

✓ Accepted answer

First off, to use "LocalPlayer", you need to be looking in players. Example:

local player = game.Players.LocalPlayer -- NOT just game.LocalPlayer

Second of all, I do recommend making this work using remote events. However, here is what I believe you need to do

function onClicked(playerWhoClicked)
    local guiName = playerWhoClicked.PlayerGui.GameName -- Game name gui, assuming everything is spelled correctly
if guiName ~= nil then -- just making sure it's there
    wait() -- There is no point of waiting 1/10 seconds, just give it a wait() so it catches up.
    guiName:Destroy() --now it's removed
end -- an extra end is now added
end

script.Parent.ClickDetector.MouseClick:connect(onClicked)

5 comments

0

Hope I helped! Please mark as answered if I did

0

The wait is pointless but the rest looks OK.

0

Yeah I've always been told that. The wait() is really useless unless in a loop. But it's a personal preference, I for whatever reason think it helps

0

It doesn't.

0

Alright thanks for the tip

LocalPlayer is a child of Players

Use game.Players.LocalPlayer instead.

LocalPlayer is not a valid member of DataModel like it says. It should be game.Players.LocalPlayer, not just game.LocalPlayer

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