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

Is there anything wrong with this part moving script?

Asked by 10 years ago

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.

2 answers

Log in to vote
2
Answered by
Perci1 4988 Trusted Moderation Voter Community Moderator
10 years ago

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)

Ad
Log in to vote
1
Answered by
iaz3 190
10 years ago
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.

Answer this question