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

Why won't this script work?

Asked by 8 years ago
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

0
You didn't call the function hide. Therefor it won't work. docrobloxman52 407 — 8y
0
I did on line 11 secretboy6 68 — 8y

1 answer

Log in to vote
2
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

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.
0
Thanks! That was my first "complicated" script on Lua so I knew the problem would be something as simple as that XD secretboy6 68 — 8y
0
@M39a9am3R Teaching users deprecated stuff I'm disappointed in YOU!! UserOnly20Characters 890 — 8y
0
*facepalm* Forgot that KeyDown is heading toward deprecation. I would look into UserInputService if I were you. M39a9am3R 3210 — 8y
Ad

Answer this question