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

This script works in studio but not Play Mode?

Asked by 8 years ago
local background = game.Players.LocalPlayer.PlayerGui.ScreenGui.BackGround
local Creditsbutton = game.Players.LocalPlayer.PlayerGui.ScreenGui.BackGround.CreditsButton
local TextLabel =game.Players.LocalPlayer.PlayerGui.ScreenGui.BackGround.TextLabel
local Playbutton =  game.Players.LocalPlayer.PlayerGui.ScreenGui.BackGround.PlayButton

function OnClicked(click)
    background.BackgroundTransparency = .4
    wait(.2)
    background.BackgroundTransparency = .8
    wait(.2)
    background.BackgroundTransparency = 1
    Creditsbutton.TextTransparency = .3
    wait(.2)
    Creditsbutton.TextTransparency = .6
    wait(.2)
    Creditsbutton.TextTransparency = .9
    wait(.2)
    Creditsbutton.TextTransparency = 1
    TextLabel.TextTransparency = .3
    wait(.2)
    TextLabel.TextTransparency = .6
    wait(.2)
    TextLabel.TextTransparency = .9
    wait(.2)
    TextLabel.TextTransparency = 1
    Playbutton.TextTransparency = .3
    wait(.2)
    Playbutton.TextTransparency = .6
    wait(.2)
    Playbutton.TextTransparency =  .9
    wait(.2)
    Playbutton.TextTransparency =  1
end

Playbutton.MouseButton1Click:connect(OnClicked)

This simple main menu script couldn't run better in studio mode. But in Play mode it's broken. Doesn't do anything. Please help?

0
Is this LocalScript or ServerScript? You can not use LocalPlayer in ServerSide scripts. Also, your code seems to be a bit inefficient, might I suggest learning to use loops? M39a9am3R 3210 — 8y
0
I don't want it to loop.. it' simple. When someone clicks the Play button, I want everything to disappear. How is it inefficient? And it doesn't need looping. Also, it's in a Sever Script. james24dj 90 — 8y
0
Well, looping is more efficient than writing the same lines over and over with different values. You could use a for loop to slowly reduce visibility of the PlayButton and other GuiObjects. for i=0, 1, .1 do Playbutton.TextTransparency = i wait() end meaning for every index value between 0 and 1 at the increment of .1 the transparency will be set to that. Just a thought. M39a9am3R 3210 — 8y
0
Maybe, but where's my issue?! You didn't help! (I'll look into looping though) james24dj 90 — 8y
View all comments (6 more)
0
"Is this LocalScript or ServerScript? You can not use LocalPlayer in ServerSide scripts." -M39a9am3R First Comment On This Question. You need to adjust the script so that it will work in ServerScripts, or move the script to a LocalScript. M39a9am3R 3210 — 8y
0
It's a serverscript inside of a textbox and Idk what a ServerSideScript is! james24dj 90 — 8y
0
I figured it out on my own.. james24dj 90 — 8y
0
No I didn't! DUDE I NEED HELP!!! ITS BROKEN!! james24dj 90 — 8y
0
Calm down, it takes more than 30 minutes to type up a full blown answer... I can tell you I am 80% with my answer right now. M39a9am3R 3210 — 8y
0
o sorry then james24dj 90 — 8y

2 answers

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

There are three types of scripts, they all do different things depending on their location, and capabilities.

  • Server Side Scripts - Mainly known as just scripts, these scripts handle objects affiliated with the server. Meaning these scripts are allowed and expected to change objects and properties in Workspace, Lighting, Players Service, and other services. These scripts mainly work in ServerScriptService, Workspace. In a FilteringEnabled settings, I have heard that Server Side Scripts do not work in PlayerGui. However this is to be expected as PlayerGuis are a member of the Player and is considered a client side action. These scripts have access to the contents of ServerScriptService, and ServerStorage whilst LocalScripts do not.

  • Local Scripts - I do not believe I can stress this enough, but the property LocalPlayer of the Players service only works in LocalScripts! Since LocalScripts handle client side actions, they are not expected to be used to modify server objects such as the Workspace, Lighting, or Player service (unless you're referring to the client). With LocalScripts you can only run the scripts in the Player's; Character, PlayerGui, Backpack, or PlayerScripts. Since LocalScripts are expected to handle client side actions, they are expected to access and modify ReplicatedStorage objects, PlayerGui, Character's humanoid and the character itself.

  • Module Scripts - This is mainly a topic for another question, but these scripts allow for you to store functions and variables and use them amongst multiple scripts. They only run once when required by script.


As for loops, these will make your code much cleaner. Having lines to do the same thing to bring a object transparency down by a decimal each time is a waste of time. Also, you will get chewed out by many scripters for not doing so. There are three types of loops, while, for, and repeat. For your question, I would recommend a for loop since these types of loops have a special third argument when being used numerically.

As for a recommendation, use variables to make finding your objects easier, instead of doing 4 four lines of game.Players.LocalPlayer.ScreenGui... you could set up a variable to do sGui = game.Players.LocalPlayer.ScreenGui Playbutton = sGui.Background.PlayButton.


Now for the final fix script, if you want to learn what every line of code does, read the comments after the --.

I can not stress this enough, this script MUST be in a LocalScript! Reason being is because you're handling the PlayerGui, and you're using LocalPlayer.

local sGui = game.Players.LocalPlayer.ScreenGui --Alright, this defines the ScreenGui, we can see it is a member of LocalPlayer
local background = sGui.BackGround --We used the variable sGui to quickly jump to background and define it as such.
local Creditsbutton = background.CreditsButton --Now CreditsButton is defined.
local TextLabel = background.TextLabel --TextLabel
local Playbutton =  background.PlayButton --and Playbutton.

function OnClicked() --There is no value given for MouseButton1Click events. So you did not need 'click'
    for i=0, 1, .1 do --Since it seems as though you're changing background transparency here, we will start a for loop, for every .1 between 0 and 1, the transparency will change.
        background.BackgroundTransparency = i --Since i is the number that continually goes up, that will define the transparency. You can change the .1 in the for loop to whatever you want, as long as it is a factor of 1 (or the second argument depending on how you use it).
        wait() --We want a wait to give it a somewhat smooth transition.
    end --Ends the for loop.

    --Same thing goes for the Creditsbutton, TextLabel, and PlayButton.

    for i=0, 1, .1 do
        Creditsbutton.TextTransparency = i
        wait()
    end

    for i=0, 1, .1 do
        TextLabel.TextTransparency = i
        wait()
    end

    for i=0, 1, .1 do
        Playbutton.TextTransparency = i
        wait()
    end
end

Playbutton.MouseButton1Click:connect(OnClicked)

With the use of for loops, I was able to knock 9 lines off the code (not counting whitespace).

If the script still does not work in Play mode, press F9, and tell me what the error is.


Hopefully this answer helped, if it answered your question then accept it, if it just helped then leave an upvote. If you have any questions feel free to leave them in the comments under this answer and I will get back to you!

"i" is essentially the index in the for loop. It means that for everything between the first index and the last index the loop will execute code in it. For the example above, for everything between 0, and 1 (.1, .2, .3, .4 etc.) the loop executed the code in it.

"v" is commonly seen in generic loops, used to go through tables, v basically means value in the case. For every index and value in pairs inside of the table then the loop executes that code. Say I wanted to destroy everything in background, you can just forget i in this case and use _ since you won't be using it. for _,v in pairs(background:GetChildren()) do v:Destroy() end. Thing about i and v in for loops is they are variables, so they can take any word. Like for k,o in pairs "k" being key or index and "o" being object, or even for _,plr in pairs(game.Players:GetChildren()) _ being a variable you won't use plr being the Player in the Players Service.

0
I've learned alot. Thank you. I'll study it and use this instead of doing what I was doing. Lol. 1 question though. What's i? And I also occasinally see v too. What do they mean? james24dj 90 — 8y
0
Lol, I don't think you realize how much you taught me just now. xD james24dj 90 — 8y
0
Edit: Moved answers to bottom of original post. I'm glad I am able to help. M39a9am3R 3210 — 8y
0
But.. sadly the script didn't work.. and they are in local scripts. james24dj 90 — 8y
0
Check the developer console, tell me if it prints any errors (they should be marked in red and blue under the tab Local Log). M39a9am3R 3210 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

Are you using WaitForChilds for PlayerGui? PlayerGui sometimes needs a bit of time to load.

local background = game.Players.LocalPlayer:WaitForChild("PlayerGui").ScreenGui.BackGround
local Creditsbutton = game.Players.LocalPlayer:WaitForChild("PlayerGui").ScreenGui.BackGround.CreditsButton
local TextLabel =game.Players.LocalPlayer:WaitForChild("PlayerGui").ScreenGui.BackGround.TextLabel
local Playbutton =  game.Players.LocalPlayer:WaitForChild("PlayerGui").ScreenGui.BackGround.PlayButton

function OnClicked(click)
    background.BackgroundTransparency = .4
    wait(.2)
    background.BackgroundTransparency = .8
    wait(.2)
    background.BackgroundTransparency = 1
    Creditsbutton.TextTransparency = .3
    wait(.2)
    Creditsbutton.TextTransparency = .6
    wait(.2)
    Creditsbutton.TextTransparency = .9
    wait(.2)
    Creditsbutton.TextTransparency = 1
    TextLabel.TextTransparency = .3
    wait(.2)
    TextLabel.TextTransparency = .6
    wait(.2)
    TextLabel.TextTransparency = .9
    wait(.2)
    TextLabel.TextTransparency = 1
    Playbutton.TextTransparency = .3
    wait(.2)
    Playbutton.TextTransparency = .6
    wait(.2)
    Playbutton.TextTransparency =  .9
    wait(.2)
    Playbutton.TextTransparency =  1
end

Playbutton.MouseButton1Click:connect(OnClicked)
0
Asker claims the script is in Playbutton, the GUI elements should have loaded by the time the local script loads. M39a9am3R 3210 — 8y

Answer this question