what this script does is it ascends and descends the 2 random rocks(just parts) in the table jumparts. The script works but the button(also a part) color does not change to green after it is done which is very bizzare any ideas? If you read the script it explains itself i made the table jumparts by using a generic loop and loop through a model in workspace then adding it to the table. No errors either.
enabled=true jumparts={} for i,v in pairs(game.Workspace.jumparts:GetChildren())do table.insert(jumparts,i,v) end function ranNum() if enabled==true then enabled=false script.Parent.BrickColor=BrickColor.new("Really red") local ranNum1=math.random(1,#jumparts) local ranNum2=ranNum1 while ranNum1==ranNum2 do ranNum2=math.random(1,#jumparts) wait() end local blockChosen=jumparts[ranNum1] local blockChosen2=jumparts[ranNum2] local chosenOne={blockChosen,blockChosen2} while jumparts~={} do for i=4,13,0.3 do blockChosen.CFrame=CFrame.new(chosenOne[1].Position.x,chosenOne[1].Position.y+1.1,chosenOne[1].Position.z) blockChosen2.CFrame=CFrame.new(chosenOne[2].Position.x,chosenOne[2].Position.y+1.1,chosenOne[2].Position.z) wait(0.1) end wait(1) for i=13,4,-0.3 do blockChosen.CFrame=CFrame.new(chosenOne[1].Position.x,chosenOne[1].Position.y-1.1,chosenOne[1].Position.z) blockChosen2.CFrame=CFrame.new(chosenOne[2].Position.x,chosenOne[2].Position.y-1.1,chosenOne[2].Position.z) wait(0.1) end table.remove(jumparts,ranNum1) table.remove(jumparts,ranNum2) if jumparts ~={} then ranNum1=math.random(1,#jumparts) ranNum2=ranNum1 while ranNum1==ranNum2 do ranNum2=math.random(1,#jumparts) wait() end blockChosen=jumparts[ranNum1] blockChosen2=jumparts[ranNum2] chosenOne={blockChosen,blockChosen2} end end enabled=true script.Parent.BrickColor=BrickColor.new("Lime green") end end function onClicked() ranNum() end script.Parent.ClickDetector.MouseClick:connect(onClicked)
You should probably fix the spacing on your code...
Let's clean up some logic here. You are doing way too much work to get random things from the list!
First, you don't need to copy the :GetChildren()
into a list at the top -- it won't work anyway; you destroy it in ranNum
, but you only get it once before.
There's a simple way to clean it up: You don't need to get two different ranNum1
and ranNum2
, you just remove before getting the second one.
We can even use the fact that table.remove
returns the thing removed to make this even shorter:
local chosenA = table.remove(jumpParts, math.random(#jumpParts) local chosenB = table.remove(jumpParts, math.random(#jumpParts)
chosenOne
doesn't even need to exist....
We can also simplify the CFrame
line; there's no need to type out blahblahblah.x, blahblahblah.y, blahblahblah.z
(if you did need to you should have made it a variable to make it brief).
Also, let's make a variable for how much it moves up / down by so that we don't need to change 4 lines if we want to make it faster or slower.
You have a while
loop -- no need to copy the body of it into an if
at the bottom!
You don't need to say == true
. It's cleaner and easier to read if enabled then
From all this cleanup, you get this, which does the same thing in much less and simpler code:
enabled = true local UP = Vector3.new(0, 1.1, 0) function ranNum() local jumpParts = workspace.jumparts:GetChildren() if enabled == true then enabled = false script.Parent.BrickColor = BrickColor.new("Really red") while jumpParts ~= {} do local chosenA = table.remove(jumpParts, math.random(#jumpParts) local chosenB = table.remove(jumpParts, math.random(#jumpParts) for i = 4, 13, 0.3 do chosenA.CFrame = CFrame.new( chosenA.Position + UP ) chosenB.CFrame = CFrame.new( chosenB.Position + UP ) wait(0.1) end wait(1) for i = 4, 13, 0.3 do chosenA.CFrame = CFrame.new( chosenA.Position - UP ) chosenB.CFrame = CFrame.new( chosenB.Position - UP ) wait(0.1) end end enabled = true script.Parent.BrickColor = BrickColor.new("Lime green") end end function onClicked() ranNum() end script.Parent.ClickDetector.MouseClick:connect(onClicked)
When you compare tables, ==
doesn't mean "contains the same things" -- it means "is the same table". {} == {}
will be false, not true
.
If you want to know if something is empty, use #list
to get the number of things in it (and make sure it's 0). In our case, we actually need to make sure we have two things, not just at least one.
The result:
enabled = true function ranNum() local jumpParts = workspace.jumparts:GetChildren() if enabled then enabled = false script.Parent.BrickColor = BrickColor.new("Really red") while #jumpParts >= 2 do local chosenA = table.remove(jumpParts, math.random(#jumpParts) local chosenB = table.remove(jumpParts, math.random(#jumpParts) for i = 4, 13, 0.3 do chosenA.CFrame = CFrame.new( chosenA.Position + UP ) chosenB.CFrame = CFrame.new( chosenB.Position + UP ) wait(0.1) end wait(1) for i = 4, 13, 0.3 do chosenA.CFrame = CFrame.new( chosenA.Position - UP ) chosenB.CFrame = CFrame.new( chosenB.Position - UP ) wait(0.1) end end enabled = true script.Parent.BrickColor = BrickColor.new("Lime green") end end function onClicked() ranNum() end script.Parent.ClickDetector.MouseClick:connect(onClicked)