generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Random

Previous Thread :: Next Thread 
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 09:07 PM
while true do
wait(0.1)
math.randomseed(tick())
for _ = 1, 10 do
print(math.random(5))
wait(1)
end
end


What I want to add is, if the number that came out matches 1, 2, 3, 4 or 5 for something different to happen
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
08 Sep 2012 09:29 PM
Why not make a table holding events then run the events randomly...

local events = {function() print("Hello, world!") end,function() print(1+1) end}

while true do
events[math.random(#events)]()
wait(1)
end
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 09:39 PM
Ok, if its local events do I need to use a LocalScript?

local events = {function() print("ExtendedBattle") end, function() print("GrudgeMatch") end, function() print("TagTeamBattle") end,function() print("RegularRound") end, function() print("RegularRound2") end, function() print("RegularRound3") end}

while true do
events[math.random(#events)]()
wait(1)
end




Would I replace the print with the actual code?
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
08 Sep 2012 09:41 PM
"Ok, if its local events do I need to use a LocalScript?"

What do you mean by "local events"?

"Would I replace the print with the actual code?"

Yes. You would write the code in a function and when the function is fired your code will run. For instance, if you wanted the baseplate to change colour, you would put this inside of the table:

function recolor() workspace.Base.BrickColor = BrickColor.Random() end
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 09:42 PM
local events is the variable isn't it?
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
08 Sep 2012 09:45 PM
I still don't understand what you mean.

an event is followed by a connection and ran when the certain event occurs. For instance:

basePart.Touched:connect(function(hit) --Event
print(hit.Name)
end)

a global variable is:

bool = false

a local variable is:

local n = 5

A local variable is only "accessible" in it's own scope, but I'd recommend you use them.
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 09:47 PM
Lastly, If you can show me the wiki or tell me these other two things.

If I wanted to add a "if 2 players are in the game then start the full script"
and
How to select a number of players.

For example, if I wanted to select 4 players out of the server, how would I make it random? The teleportation wiki isn't telling me this.
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
08 Sep 2012 10:00 PM
    function selectPlayers(n)
        if game.Players.NumPlayers => n then
            local chosen = {}
            for i = 1,n do
                local found = math.random(game.Players.NumPlayers)
                if not chosen[found] then
                    local player = game.Players:GetPlayers()[found]
                    table.insert(chosen,found,player)
                end
            end
            return chosen
        end
    end

then, to use that to advantage use we can do...

    for player in selectPlayers(4) do
        player.Character.Head:Destroy() --Would remove all the chosen player's heads.
    end
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 10:32 PM
I tried modifying that and it crashed. I would change the second line to my preferences. After that (for example the 6th line) I don't know how that affects the game. What I want to do is take around 3 or 4 people and teleport them. For example, if I wanted a simple 1 vs 1 sword fight. How would I make the variable for the selected players?

function selectPlayers(n)
if game.Players.NumPlayers => n then
local chosen = {}
for i = 1,n do
local found = math.random(game.Players.NumPlayers)
if not chosen[found] then
local player = game.Players:GetPlayers()[found]
table.insert(chosen,found,player)
end
end
return chosen
end
end
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
08 Sep 2012 10:35 PM
"I tried modifying that and it crashed."

Well, what did you modify?

"For example, if I wanted a simple 1 vs 1 sword fight. How would I make the variable for the selected players?"

then you would do something along the lines of:

local player1,player2 = unpack(selectPlayers(2))
print(player1.Name,player2.Name)
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 10:46 PM
"Well, what did you modify?"

local events = {function() print("TagTeamBattle")
function selectPlayers(n)
if game.Players.NumPlayers => n then
local chosen = {}
for i = 1,n do
local found = math.random(game.Players.NumPlayers)
if not chosen[found] then
local player = game.Players:GetPlayers()[found]
table.insert(chosen,found,player)
end
end
return chosen
end
end
, function() print("RegularRound") end}

while true do
events[math.random(#events)]()
wait(1)
end




I would modify to this

local events = {function() print("TagTeamBattle")
if game.Players.NumPlayers => 2 then
local player1 = math.random(game.Players.NumPlayers)
local player2 = math.random(game.Players.NumPlayers)
table.insert(chosen,found,player)
end
, function() print("RegularRound") end}

while true do
events[math.random(#events)]()
wait(1)
end
Report Abuse
nate890 is not online. nate890
Joined: 22 Nov 2008
Total Posts: 21686
08 Sep 2012 10:49 PM
What what what????

Err....

I gave you a function that selects random players. Plus when you modified it, that wouldn't even work. I'm thinking you should become more expirienced in the art of programming before trying to comprehend this.
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 10:50 PM
I understand scripting to a inermediate level. This is my first time using math.random and Players on the same script.
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 10:56 PM
local events = {function() print("TagTeamBattle")
function selectPlayers(n)
if game.Players.NumPlayers => n then
local chosen = {}
for i = 2,n do
local found = math.random(game.Players.NumPlayers)
if not chosen[found] then
local player1 = players[math.random(#players)][found]
local player2 = players[math.random(#players)][found]
table.insert(chosen,found,player)
end
end
return chosen
end
end
, function() print("RegularRound") end}

while true do
events[math.random(#events)]()
wait(1)
end
Report Abuse
JayTheDJ is not online. JayTheDJ
Joined: 07 Aug 2011
Total Posts: 262
08 Sep 2012 11:08 PM
Bump.

I know now.

local events = {function() print("TagTeamBattle")
function selectPlayers(n) -- The function
if game.Players.NumPlayers > n then -- If the NumPlayers property is n
local chosen = {} -- The table
for i = 4,n do -- I think this is if the NumPlayers = the selected value
local found = math.random(game.Players.NumPlayers) -- Picks random
if not chosen[found] then -- If the randomly picked is not in local chosen already
local player = game.Players:GetPlayers()[found] -- Picks again
end
end
return chosen
end
end
end
, function() print("RegularRound") end}

while true do
events[math.random(#events)]()
wait(1)
end
Report Abuse
Cheater is not online. Cheater
Joined: 29 Jun 2007
Total Posts: 5258
09 Sep 2012 03:15 AM
How should that work if you're even missing an ending bracket at the first line?
Report Abuse
Cheater is not online. Cheater
Joined: 29 Jun 2007
Total Posts: 5258
09 Sep 2012 03:15 AM
Oh nvm, forget that post.
Floodcheck twice.
Report Abuse
Cheater is not online. Cheater
Joined: 29 Jun 2007
Total Posts: 5258
09 Sep 2012 03:16 AM
But I think you can't define a function inside a table. I've never seen someone doing this before.
Report Abuse
Nartle is not online. Nartle
Joined: 29 Mar 2008
Total Posts: 7202
09 Sep 2012 03:16 AM
He put the bracket after an end...?
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image