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

Why won't this script work?

game.StarterGui.ScreenGui.Frame.Visible = true

function hide()
	game.StarterGui.ScreenGui.Frame.Visible = false
end

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.KeyDown:connect(function(Key)
    if Key == "u" then
	hide()
    end
end)

It's a script that hides a frame when the "u" key is pressed, but it won't work...

Helpful info: -The script is in workspace (I think that's the problem, cause I don't see any problems in the script itself0

2 comments

0

You didn't call the function hide. Therefor it won't work.

0

I did on line 11

1 answer

✓ Accepted answer

First off, your script should be a LocalScript. These scripts will deal with the client objects such as GUIs and the Player's Mouse. LocalScripts will only work in PlayerGui, Backpack, PlayerScripts, and the Player's Character.

Secondly, you're changing the visibility of a frame in StarterGui, not in PlayerGui. Meaning, if you click to hide it, the GUI will still show on screen until you respawn. To fix this you want to use the PlayerGui, and since you should be using a LocalScript, LocalPlayer should work. LocalPlayer will only work in LocalScripts.

Now to begin fixing your code...

game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true --LocalPlayer is a value of the Players Service, but only for LocalScripts. PlayerGui is a container of GUI objects inside of the Player, this means that any GUI elements added, changed, or removed will change real time with the client.

function hide()
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = false
end

local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Mouse.KeyDown:connect(function(Key)
    if Key == "u" then
        hide()
    end
end)

Once again, LocalPlayer will not work inside a regular script. Also, LocalScripts will only work in PlayerScripts, PlayerGui, Backpack, and the Player's Character. So make sure some of these issues get resolved.

######If this question helped, leave an upvote I would appreciate it. And if it solved your problem, then hit that accept answer button so you and I both get a reputation point.

3 comments

0

Thanks! That was my first "complicated" script on Lua so I knew the problem would be something as simple as that XD

0

@M39a9am3R Teaching users deprecated stuff I'm disappointed in YOU!!

0

facepalm Forgot that KeyDown is heading toward deprecation. I would look into UserInputService if I were you.

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