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: Making a ball move in 9 different ways

Previous Thread :: Next Thread 
pokemaniacX is not online. pokemaniacX
Joined: 27 Apr 2008
Total Posts: 13487
26 Nov 2011 11:25 AM
I'm trying to make it so that a ball chooses to move in one of 9 different ways, so I try some mathrandom stuff, and nothing happens.

local m = math.random(1,9)
while true do
if m == 1 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 1)
end
while true do
if m == 2 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.5)
end
while true do
if m == 3 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.5)
end
while true do
if m == 4 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 1)
end
while true do
if m == 5 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.75)
end
while true do
if m == 6 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.75)
end
while true do
if m == 7 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.5)
end
while true do
if m == 8 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 1)
end
while true do
if m == 9 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.75)
end

What's the solution?
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
26 Nov 2011 11:30 AM
instead of doing all of those, just do one 'while true do' at the beginning, and make each if except for the first one 'else if' and put all of the ends at the end of the script. like so:

m = math.random(1,9)
while true do
if m == 1 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 1)
else if m == 2 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.5)
else if m == 3 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.5)
else if m == 4 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 1)
else if m == 5 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.75)
else if m == 6 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.75)
else if m == 7 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.5)
else if m == 8 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 1)
else if m == 9 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.75)
end
end
end
end
end
end
end
end
end
end
Report Abuse
MrChubbs is not online. MrChubbs
Joined: 14 Oct 2010
Total Posts: 4969
26 Nov 2011 11:30 AM
--local m = math.random(1,9) This defines it once and only once.
while true do
local m = math.random(1,9)
if m == 1 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 1)
end
while true do
if m == 2 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.5)
end
while true do
if m == 3 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.5)
end
while true do
if m == 4 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 1)
end
while true do
if m == 5 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.75)
end
while true do
if m == 6 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.75)
end
while true do
if m == 7 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.5)
end
while true do
if m == 8 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 1)
end
while true do
if m == 9 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.75)
end
or

while wait(1) do
local X = math.random(0,1)
local Y = math.random(0,1)
local Z = math.random(0,1)
script.Parent.Velocity = Vector3.new(X*5, Y*5, Z*5)
end
Report Abuse
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
26 Nov 2011 11:31 AM
Woahwoah! Only one loop!

First, look through and any spot you see a:

end
while true do
if

Replace all of that with 'elseif' (no apostrophe)

Next, at the very end of the script, add an end

Now delete wherever it says 'wait(0.01)'

Then put a wait(.01) after the first while true do

Last, put the math.random thing after the first while true do.
Report Abuse
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
26 Nov 2011 11:32 AM
You guys should use elseif and only one loop.
Report Abuse
pokemaniacX is not online. pokemaniacX
Joined: 27 Apr 2008
Total Posts: 13487
26 Nov 2011 11:34 AM
killjoy's works. I can't believe I forgot to use "else"! >_<
Report Abuse
killjoy37 is not online. killjoy37
Joined: 27 Aug 2008
Total Posts: 2821
26 Nov 2011 11:35 AM
Yay, im glad mine worked first try. (It's a rare event)
Report Abuse
LevelNoobX is not online. LevelNoobX
Joined: 18 Sep 2011
Total Posts: 565
26 Nov 2011 11:36 AM
local m = math.random(1,9)
while wait() do
if m == 1 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 1)
elseif m == 2 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.5)
elseif m == 3 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.5)
elseif m == 4 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 1)
elseif m == 5 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.75)
elseif m == 6 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.5, 0, 0.75)
elseif m == 7 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 0.5)
elseif m == 8 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(0.75, 0, 1)
elseif m == 9 then
wait(0.01)
script.Parent.Position = script.Parent.Position + Vector3.new(1, 0, 0.75)
end end
Report Abuse
MrChubbs is not online. MrChubbs
Joined: 14 Oct 2010
Total Posts: 4969
26 Nov 2011 11:36 AM
I still like mine better.
Report Abuse
MrChubbs is not online. MrChubbs
Joined: 14 Oct 2010
Total Posts: 4969
26 Nov 2011 11:37 AM
Forgot to add the -1 XD


while wait(1) do
local X = math.random(-1,1)
local Y = math.random(-1,1)
local Z = math.random(-1,1)
script.Parent.Velocity = Vector3.new(X*5, Y*5, Z*5)
end
Report Abuse
TheMyrco is not online. TheMyrco
Joined: 13 Aug 2011
Total Posts: 15105
26 Nov 2011 11:39 AM
LOl I'd do it like this:

local part = FILLINOBJECT
local directions = {
Vector3.new(0, 6, 0);
Vector3.new(0, -5, 0;
Vector3.new(6, 6, 0)
--etc.....
}

while wait() do
part.CFrame = part.CFrame + directions[math.random(1, #directions)]
end


~Myrco; Music lover, nederlands/dutch and a scripter
Report Abuse
BunnyBoy26 is not online. BunnyBoy26
Joined: 17 Jun 2010
Total Posts: 5674
26 Nov 2011 11:49 AM
I'll shorten it up:

function move(x,z)
script.Parent.CFrame = script.Parent.CFrame + Vector3.new(x,0,z)
end

while wait() do
local m = math.random(9)
if m == 1 then
move(.5,1)
elseif m == 2 then
move(1,.5)
elseif m == 3 then
move(.5,.5)
elseif m == 4 then
move(1,1)
elseif m == 5 then
move(.75,.75)
elseif m == 6 then
move(.5,.75)
elseif m == 7 then
move(.75,.5)
elseif m == 8 then
move(.75,1)
elseif m == 9 then
move(1,.75)
end
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