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

How do I launch a random function within a table (in a loop)?

Asked by 9 years ago

Hello , I'm trying to launch random functions that are in a table. This is what I'm currently trying: I changed some of the code

c=script.Parent
L1=c.Lane1
L2=c.Lane2
L3=c.Lane3
L4=c.Lane4


 Key = {}
Key.Left = function (Left) L1.A1:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)end
Key.Down = function (Down) L2.A2:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)end
Key.Up = function (Up) L3.A3:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)end
Key.Right = function(Right) L4.A4:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)end

wait(1)
while true do
Key[math.random(#Key.Left,Key.Down,Key.Right,Key.Up)]()
end


This is the output :

Players.Player1.PlayerGui.GuiGame.DDR.Script:16: attempt to get length of field 'Left' (a function value)

0
@trytoeatme, oh that was an error I made when trying to copy and paste the script to here. I dont think that was the issue. SamDomino 65 — 9y
0
ah, okay. one sec. TopDev 0 — 9y
0
18:49:48.636 - Players.Player1.PlayerGui.GuiGame.DDR.Script:7: attempt to call a userdata value 18:49:48.636 - Stack Begin 18:49:48.636 - Script 'Players.Player1.PlayerGui.GuiGame.DDR.Script', Line 7 - global Pat 18:49:48.637 - Script 'Players.Player1.PlayerGui.GuiGame.DDR.Script', Line 23 18:49:48.637 - Stack End SamDomino 65 — 9y

1 answer

Log in to vote
0
Answered by
TopDev 0
9 years ago

Here you go.

I tested it, it's working perfectly.

c=script.Parent
L1=c.Lane1
L2=c.Lane2
L3=c.Lane3
L4=c.Lane4
function Key1()
    L1.A1:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)
end
function Key2()
    L2.A2:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)
end
function Key3()
    L3.A3:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)
end
function Key4()
    L4.A4:TweenPosition(UDim2.new(0,0,-0.137,0)"Out","Quad",4)
end

Fire={Key1,Key2,Key3,Key4}
while wait() do
Pat = Fire[math.random(1, #Fire)]
Pat()
end

0
The definition of `Fire` would better be placed before the loop (line 19) BlueTaslem 18071 — 9y
0
Oh, right. I fixed it. TopDev 0 — 9y
Ad

Answer this question