I'm trying to move a part to one of two locations. The location is randomly selected from a list of locations in an array. I'm using the code below, but it's not working. Is there anything I should change?
function onClicked(playerWhoClicked) local myArray = {"CFrame.new(0, 50, 0)", "CFrame.new(0, 25, 0)"} script.Parent.CFrame = CFrame.new(math.random(1, #myArray)) end script.Parent.ClickDetector.MouseClick:connect(onClicked)
Any help is greatly appreciated.
I think your problem is that you just put two strings in you table. CFrame.new()
does not take string values. On top of that, even if we ignore the fact that CFrame.new()
doesn't take strings, you're basically doing this:
CFrame.new(CFrame.new(0,50,0))
which also will not work, for obvious reasons.
Try doing this:
function onClicked(playerWhoClicked) local myArray = {CFrame.new(0, 50, 0), CFrame.new(0, 25, 0)} script.Parent.CFrame = myArray[math.random(1,#myArray)] end script.Parent.ClickDetector.MouseClick:connect(onClicked)
function onClicked(playerWhoClicked) local myArray = {CFrame.new(0, 50, 0), CFrame.new(0, 25, 0) } script.Parent.CFrame = myArray[math.random(1, #myArray)] end script.Parent.ClickDetector.MouseClick:connect(onClicked)
This will work. It will randomly select an index to one of the values., which the CFrame can be set to. You can add more.