I'm using this code in a normal script (not local), yet for some reason, works in studio but not on a server/game. The script makes a button (the script's parent) not visible if a value is true.
if script.Parent.Parent.Parent.Parent.Name == "PlayerGui" then script.Parent.Parent.Parent.Parent.Parent.PlayerScripts:WaitForChild("HasBase") if script.Parent.Parent.Parent.Parent.Parent.PlayerScripts.HasBase.Value == true then script.Parent.Visible = false else script.Parent.Visible = true end end script.Parent.MouseButton1Click:connect(function() script.Parent.Parent.Parent.Parent.Parent.PlayerScripts:WaitForChild("HasBase") script.Parent.Visible = false script.Parent.Parent.Parent.Parent.Parent.PlayerScripts.HasBase.Value = true end)
Does anyone know why and how to fix it? There are no errors in the output.
People all too commonly disregard the difference between local scripts
, and server scripts
, and just categorize them as the same thing. They're not.
It is very important (if not in most cases, required) to use the right script for the job. Here's a brief description of what a local script is, vs what a server script is:
Local Script
local script
is a program that will run it's code directly on the player's computer (i.e, no internet connection required). If you've ever disconnected from a game before, but still saw particles or animations running in the background, that's because those programs are running locally to your computer.Server Script
server script
is a program that runs it's code directly on the server. Whenever a change is made to the server, that change will then replicate to all clients connected to the server (your computer). Because this change is taking place over an internet connection, there's going to be a small interval of delay. This is called Network latency
(or just latency if used in the right context).You should care because these scripts are built for their intended tasks. You shouldn't use a server script to do something that's specific to a client's computer, like running an animation or collecting input from the user, and you should't use a local script to do something that is meant to centralize information within the server.
Now, there are cases when you will want to use a local script to make something happen on the server, and to do this you'd use remotes
.
The answer is really simple actually. Everything in studio is local. When you're in test mode, your entire game is running on your computer without any server connections (with the exception of other potential background tasks). So even code in your server scripts, will run locally.
Simple. Anything that relates to user input (i.e, a key press or a mouse click), anything that involves GUIs, animations, weapons, ect, should be handled with a local script. Basically anything the user interacts with, or is private for the user.
Anything that relates to acting directly on the server, for example, using DataStoreService
or handling product purchases. The use of server scripts are not limited to these examples, as anything you want to replicate to all players or save in a single place should be done with a server script.
In summary, I highly suggest you read up on the differences between these two scripts if this wasn't clear initially. If you have any questions, just let me know.
You can try this, and there's no need to put random WaitForChild()'s.
HasBase = script.Parent.Parent.Parent.Parent.Parent.PlayerScripts:WaitForChild("HasBase") if script.Parent.Parent.Parent.Parent.Name == "PlayerGui" then if HasBase.Value == true then script.Parent.Visible = false else script.Parent.Visible = true end end script.Parent.MouseButton1Click:connect(function() script.Parent.Visible = false HasBase.Value = true end)
If this does not work, try printing script.Parent.Parent.Parent.Parent.Name and making sure that it is 'PlayerGui'. This is a common problem of why scripts work in studio but not in-game. Make sure you're also checking F9 > Server Log to see if any errors pop up.