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

Why does my script only work in studio and not on a server/game?

Asked by 8 years ago
Edited 8 years ago

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.

2 answers

Log in to vote
1
Answered by 8 years ago
Edited 8 years ago

Use the right script for the job


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

    • A 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

    • A 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).

Why should you care?

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.

So why does it work in studio?

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.

So how do I know which one to use?

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.

Summary

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.

0
Thank you so much! This helped a lot and improved my understanding of LocalScripts. I've changed my script to a LocalScript and now it works! Again, thank you so much for providing such an informative answer! coolyoshipower 42 — 8y
1
Glad it helped! I'd also recommend checking out these links: LocalScript - http://wiki.roblox.com/index.php?title=API:Class/LocalScript / LocalPlayer - http://wiki.roblox.com/index.php?title=API:Class/Players/LocalPlayer ScriptGuider 5640 — 8y
Ad
Log in to vote
1
Answered by
shayner32 478 Trusted Moderation Voter
8 years ago

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.

0
Tried your script, however, still doesn't work. I'm trying to print the parent - works in studio, however nothing happens in the server log ingame. coolyoshipower 42 — 8y

Answer this question