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
local background = sGui.BackGround
local Creditsbutton = background.CreditsButton
local TextLabel = background.TextLabel
local Playbutton = background.PlayButton
function OnClicked()
for i=0, 1, .1 do
background.BackgroundTransparency = i
wait()
end
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.
10 comments
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?
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.
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.
Maybe, but where's my issue?! You didn't help! (I'll look into looping though)
"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.
It's a serverscript inside of a textbox and Idk what a ServerSideScript is!
I figured it out on my own..
No I didn't! DUDE I NEED HELP!!! ITS BROKEN!!
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.
o sorry then