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

How come when I press z this function will not delete a gui?

I have received help earlier but I'm not on with coding.. I'm basically making it If I press Z it'll delete a Gui I make appear in a different script. When I press Z in game nothing happens? Much appreciated if helped

pcall(game:GetService("UserInputService").InputBegan:connect(function(key)
if key.KeyCode == Enum.KeyCode.Z then
	print 'the key Z has been pressed'
	local Gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild(One)
	if Gui then Gui:Destroy() end
else
	print 'Y u no press Z?'
end
end))

7 comments

0

Why have you put this in a pcall?

0

Because I when I press Z I want to destroy the gui

0

You dont need a pcall for that

0

What do you mean?

0

remove the pcall

0

Still doesn't work?

0

Remove the pcall. Is this a Server Script, if so it will not take UserInput.

1 answer

✓ Accepted answer

The problem is that in this:

local Gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild(One)

One refers to the variable named One, whereas in this:

local Gui = game.Players.LocalPlayer.PlayerGui:FindFirstChild("One")

"One" is a string literal. To fix this, replace One in that line with "One" (I probably should have mentioned this earlier)

Additionally, you do not need to (and should not) use pcall in this code.

7 comments

0

Yes someone told me not use pcall.. Its saying it has a issue with the " end)) "?

0

The pcall would cause the script to not work. Just remove the 'pcall(' at the start and then the ')' at the end.

0

Still nothing I removed the "pcall(" and the " ) " and the script still doesn't work. Also thanks with your patience

0

I guess its not recognizing Im pressing Z? The local script is located in a spawnlocation if that could be a issue?

0

Give him the whole edited code :P

0

You should put the local script in the StarterPlayerScripts

0

OH GOD IT WORKED I put the script in StarterPlayerScripts. Thank you so much :) Have a great day

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