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

What is wrong with my code?

Asked by 9 years ago

When I run it in Run mode on Studio, I get this error:

 Players.Player1.PlayerGui.HUD.Civilian.Buttons.LocalScript:15: attempt to call method 'MouseButton1Click' (a userdata value)

Additionally, I'm also getting this error:

Players.Player1.PlayerGui.HUD.Civilian.Buttons.LocalScript', Line 15

Here's the code:

plr = game.Players.LocalPlayer
---------------------------------------------------------
while true do
plr.PlayerGui.HUD.Civilian.Buttons.Notifications.Button:MouseButton1Click():connect(function()
if script.NotifActivated == false
then
plr.PlayerGui.HUD.Notifications:TweenPosition(UDim2.new(0,0,0,0),"Out","Linear",2,true)
if script.NotifActivated == true
then
plr.PlayerGui.HUD.Notifications:TweenPosition(UDim2.new(1,0,0,0),"Out","Linear",2,true)
script.NotifActivated = false
end 
end
end)
end 

And, here's a picture of my GUI hierarchy: http://prntscr.com/7qbord

The code is meant to check if "NotifActivated" is checked, and if it is, the Notifications bar is meant to slide into place. If it isn't, it is meant to slide out.

Thanks in advance for any help!

Edit: Formatting

1
For future reference, please make your title relevant to the issues at hand (whether it be the problem or type of script). It helps us help you! RoboFrog 400 — 9y

1 answer

Log in to vote
2
Answered by 9 years ago

There are multiple errors in your code here, I will go through each of them one by one and then provide the fixed script at the bottom of my answer.

Firstly, you're attempting to use MouseButton1Click as a method and not as an event. In basic terms, you're using a colon where you should be using a dot. Colons are only used for calling functions in Instances or when using custom functions, but you don't use colons when referencing an event in an Instance.

To fix this, just replace the colon before MouseButton1Click with a dot and remove the parenthesis after MouseButton1Click.

Secondly, you should also space out your code by using the Tab key, this makes your code easier to read. There's a whole blog article about cleaning up code written by CodeTheorem here.

Thirdly, you don't need the while true do loop before your event connection as the function is already connected to the event and that event will fire multiple times already, so there is no need to make multiple connections for it.

Finally, you need to have .Value after NotifActivated as NotifActivated is not a property of the LocalScript, but a value object. This will make sure that NotifActivated's value is checked, instead of if the value is there.

Your script should now look something like this:

plr = game.Players.LocalPlayer
---------------------------------------------------------
plr.PlayerGui.HUD.Civilian.Buttons.Notifications.Button.MouseButton1Click:connect(function()
    if script.NotifActivated.Value == false then --Using value as NotifActivated is a value object.
        plr.PlayerGui.HUD.Notifications:TweenPosition(UDim2.new(0,0,0,0),"Out","Linear",2,true)
    else --Using else to shorten code and reduce the amount of ends needed.
        plr.PlayerGui.HUD.Notifications:TweenPosition(UDim2.new(1,0,0,0),"Out","Linear",2,true)
        script.NotifActivated = false 
    end
end)

I hope my answer helped you. If it did, be sure to accept it.

Ad

Answer this question