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

Script works in studio but not online?

Asked by 8 years ago

So I have my local script here:

local replicatedstorage = game:GetService("ReplicatedStorage") 
local hat = replicatedstorage:WaitForChild("KnightHelmet") 
local plr = game.Players.LocalPlayer 
local char = plr.Character 


script.Parent.MouseButton1Down:connect(function() 
        wait(1) 
        if(char and plr and char:FindFirstChild("Humanoid")) then 
            for i, v in pairs(char:GetChildren()) do 
                if(v) then 
                    if(v:IsA("Hat")) then 
                        v:Destroy() 
                    end 
                end 
            end 
            local clone = hat:Clone() 
            clone.Parent = char      
        end  
    end)

and when I test it in studio, it works just fine. However, when I play the game online, it doesn't do anything. Now I've already read the "It works in Studio, but not online!" article in the Scripting Helpers Blog, but I'm just not sure what I can change to make it work!

2 answers

Log in to vote
0
Answered by
DevSean 270 Moderation Voter
8 years ago
local char = plr.Character

You're problem will be this line here, in studio your character will load before the script.

On a server however, the script will load before your character which means this variable will be set to nil.

repeat 
    wait()
until plr.Character ~= nil
local char = plr.Character

Waiting until the character exists should fix the issue,

0
Thanks a lot! kelimeguy 60 — 8y
Ad
Log in to vote
0
Answered by 8 years ago

When a player connects to an online ROBLOX game, sometimes it can feel like magic. However, in the background, there are actually two entirely separate versions of your game: the one that the player sees, and the one that the server sees. Every player has their own client world, and there is only one server world. For this example, we're going to assume that this server only has one player connected to it.

The world on the server will exist already when the server starts, but when a player joins, it will need to download the world from the server. Sometimes, LocalScriptLocalScripts will download to the client world and begin to run before other objects have been downloaded. This is a very common problem. To combat this, you should always use WaitForChild when dealing with other objects in initial execution code. This isn't necessary if you know the code you're writing is running once the game has already been established, such as an event like Touched.


local gui = script.Parent:WaitForChild("MyTextLabelName")

However, the reverse can also occur. If you have a script that was listening to the CharacterAdded event, for example, the event could have already fired before your function actually hooked into that event. It's usually good practice to always assume that the game has been running for a while, and you should run your event function manually on the objects (if they exist) your code deals with before hooking up the function connection.


function charAdded(model) -- Do stuff end if player.Character then charAdded(player.Character) end player.CharacterAdded:connect(charAdded)

By default, when the FilteringEnabled property of WorkspaceWorkspace is set to false, any change that happens to one of these worlds is instantly sent to the other one. This is called "network replication". An important thing to note is that because the internet isn't instant, it will take time for changes and events to replicate over the network. I'll talk more about this in a bit.

When FilteringEnabled is set to true, however, things get a bit different. Changes from the client world will no longer replicate over the network to the server. (The server changes will still replicate to the client world.) You can still communicate with the server from the client world by using RemoteEventRemoteEvents and RemoteFunctionRemoteFunctions.

0
That's an exact copy of the article. kelimeguy 60 — 8y
0
So... LegendaryBossYT 0 — 8y

Answer this question