Hello everyone. I know the answer to my question is probably obvious, but I am new to scripting and a little confused. Do scripts and local scripts run when a game starts, or do you have to activate them with
a line of code like a click detector. Is there a way to make a script always running? Thanks for reading.
Scripts will run when the game starts unless you have disabled it. To have a script constantly run, put it in a loop.
Scripts can be disabled by default before the game start and to script do always fun but if there a function like an event, if the event is fired, the function from the script will fire also, note that only the function from the script will fire, you can add multiple functions to a script with this.
1 script fully scripted with functions can be the main and only script of a game.
Youtube can help you a lot of this: https://www.youtube.com/watch?v=yc_d8Zh95l0
Ok. I have a local script in the workspace with a while true do loop but nothing in the script is running. I put some print statements in different parts of the script and not even a print statement at the top prints so I was a bit confused
LocalScript and Scripts are not the same thing at all.
Script run to the ServerSide.
LocalScript run to the ClientSide.
LocalScript are mostly use for UI/LocalSounds/PlayerStarterScript.
Script are mostly use for main game actions or events.
Althougth LocalScripts can be manipulated to run on the ServerSide with remote events.
So should this local script run when the game starts?
print(“Beginning”)
local player = game.Players.LocalPlayer
local amount = 2
local timedelay = 4
currencyname = “Currency”
while true do
if player.Team.Name == “Working” then
wait(timedelay)
for i,v in pairs(game.Players:GetPlayers()) do
if v: FindFirstChild(“leaderstats”) then
v.leaderstats[currencyname].Value = v.leaderstats[currencyname].Value + amount
end
end
end
end
This should be a serverside as its change leaderstats, if its a localscript only the player will see his own change, not the servers or others players.
But Game.Players.LocalPlayer
dont work on ClientSide Scripts
You said “if v: FindFirstChild(“leaderstats”) then”
In line 11, this is not a good practice but it doesn’t do anything different.
It should run perfectly if I’m correct, although where is this script located?
Although if he wanted to, and use a part to activate this, he could use a BindableEvent.
I just moved the script to a server script instead of a local script. It is currently in server script service.
So it should work now, remember to print constant checks, using type().
Here an example and fix of your script, but it in a ServerSide Script on ServerScriptService:
print("Beginning")
local amount = 2
local timedelay = 4
currencyname = "Currency"
while wait(timedelay) do
for cool,example in pairs(game.Players:GetChildren()) do
if example.Team.Name == "Working" then
example:WaitForChild("leaderstats")[currencyname].Value = (example:WaitForChild("leaderstats")[currencyname].Value + amount)
print(example.Name.." Got paid: "..amount)
end
end
end
Make sure to create a leaderstats folder and Currency value before.
That fixed the script I was using. I’m very new to this and really don’t know a lot about scripting. The only thing I really know how to do currently is make a variable and some other simple stuff.
Thanks for everyone explaining how this works.
This site may help you at lot: https://developer.roblox.com/en-us/
As well as Youtube and the Dev Forums here.
Good Luck with your future projects!
That’s not how remotes work. LocalScripts stay client-sided, server scripts stay server-sided. Remotes are used to pass a message with data that crosses the client-server boundary. The server can block or perform actions based on when it receives this message and can use the data to help it make decisions.
LocalScripts don’t just get “manipulated to run on the server-side”. That would imply you can perform server-side actions from a LocalScript. LocalScripts never have access to the server-side. The client makes requests for the server to do something.
Sorry, I misused context, I meant it sends messages that recreate things on the server side, or in simpler terms, makes things happen for everyone when not everyone can see it.