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

Can I use a variable throughout the entire script?

Asked by 10 years ago

I am quite new to roblox scripting, I'm experienced with A similar language (Zero) in the past but don't know what to do here

This script is making a time variable for the entire scripts

time = 0  -- A time Variable for throughout the entire Workspace
while true do -- True boolean to repeat it all
    time = time + 1 -- add 1 to the time
    wait(1) -- wait .9 seconds in order to not overload the script
    if time == 820 then -- Makes sure the round can't go longer than 12 minutes
        time = 0 -- sets the time Variable back to 0
    end --ends the if statement
    print(time) --Prints "time" for debugging
end -- ends the While true

this is one of the many places I'm using that script

playerNames = game.Players:GetChildren() --Find the names of all the players
function kill() -- Kill all players
    for i = 1, #playerNames do --repeat until all the players are killed
        game.Workspace[playerNames[i].Name].Humanoid.Health = 0 --kill the player
    end ----finished
end -- Ends the kill() function

while true do
    if time >= 90 then --Time for people to choose character
        kill() -- Runs the kill() function

        if time >= 820 then
            for i=1, #playerNames do
                playerNames[i].TeamColor = BrickColor.new("White")
            end --Ends the "for" statement          
            kill() -- Runs the kill() function
        end --Ends this "if" statment
    end -- Ends the "if" statement
end
0
On line 3, get a new list of players to kill instead of only using the initialized list which won't include everyone. Something like "for i, v in pairs(game.Players:GetChildren()) do" . GoldenPhysics 474 — 10y
0
To make it clear, I want to know how to use 1 variable throughoutt the entire script dlange95 15 — 10y

3 answers

Log in to vote
0
Answered by
ipiano 120
10 years ago

You mean that you want one variable that is available in every script in your game, correct?

There's two ways you can do this that I know of.

  1. Create an IntValue(Or NumberValue) in game.Workspace and set its value each time through the timer loop, then you can read that value in other scripts

  2. Use _G, the global variable table. I don't know much about it, but there's more info here: http://wiki.roblox.com/index.php?title=Function_Dump/Core_Functions#G

Ad
Log in to vote
0
Answered by
Lacryma 548 Moderation Voter
10 years ago

If it's the same script then yes, just define it outside any scope or omit any local from it. However, if it is a different script you will need to use _G which is Lua's global table.

_G["time"] = 0

You can reference to it by:

if _G["time"] > 1 then end

Read more about it here

0
Thank you, I tryed _G earlier but nobody told me anything about it. the link to the wiki was really helpfull dlange95 15 — 10y
Log in to vote
0
Answered by 10 years ago

@ipiano, I dont think he wants it global. He only wants it local i believe so do.

local time = 0

Explaination. putting local infront of time makes it a script value. but _G would make that value usuable thru every script.

Answer this question