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?
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.
"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.
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)