Roblox defines a local script as follows.
A LocalScript is a Lua source container that runs Lua code on a client connected to a Roblox server. They are used to access client-only objects, such as the player's Camera. For code run through LocalScripts, the LocalPlayer property of the Players service will return the player whose client is running the script.
A LocalScript will only run Lua code if it is a descendant of one of the following objects:
A Player's Backpack, such as a child of a Tool
A Player's character model
A Player's PlayerGui
A Player's PlayerScripts.
The ReplicatedFirst service
Using this information we can conduct a couple of experiments
using the various places we can use local scripts. I created a
script in ServerScriptService called mainScript and a leaderstats
folder for the player with a Gold value set to 10. Remember that
this script is running on the server.
Test 1 - Replicated First
I placed a local script in the ReplicatedFirst folder and added the
following code.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local pStats = player:WaitForChild("leaderstats")
local pGold = pStats.Gold
print("player gold = ",pGold.Value)
When I ran it I got the following error message.
Infinite yield possible on 'Players.CrazyBillTheFarmer:WaitForChild("leaderstats")'
This does not mean there is a problem with the code I wrote, but
rather WHEN it ran. As suggested replicated first will run local
scripts here BEFORE anything else, which means my mainScript
on the server has not yet run, and this causes an error.
Test 2 - StarterPack or Players Backpack, StarterGui or
PlayerGui, StarterPlayerScripts, and StarterCharacterScripts
Let’s move the same script with no changes to the rest of the
folders and run the same code. You will see that the code runs fine
and we get the correct print message.
Test 3 - Changing Values On The Client
Now we will try to cheat, and give our player some gold by writing
a for loop that increases their gold by 10 for 10 loops. You will
seed that leaderstats shows 110 Gold, and the print message
shows 110 gold. However did we really make any changes? The
answer is no. What you see here is only happening on the client
computer. Let’s add a part to workspace, that when touched will
print what the value of Gold is on the server.
What you will find is Gold still remains at the value of 10. This is an
important aspect or using Local Scripts and understanding the
difference about what is happening on the client, and what is
happening on the server.
Test 4 - Communicating With The Server From A Local Script
So it we want our server value to change then how do we do it?
The answer is to use a RemoteEvent or a RemoteFunction. These
two items act as a gateway from the client to the server. The main
difference between the two is that a remote event is a one way
message, whereas the remote function can return a message back
to the client.
Using a remote event we can trigger a function on the server that
will then change the server pGold.Value.
References
Comentários