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

Clones but does not go to CFrame?

Asked by
Hero_ic 502 Moderation Voter
8 years ago

I made a script that waits a random time between .5 and 3 seconds then clones a model. My problem is my script clones the model (I already set the primary part) but it does not change CFrame and only clones once. Anyway to fix this? Thanks. (There are parts in the workspace named G#Number)

while wait(math.random(.5,3)) do
    local Wrecker = game.ServerStorage.Wrecking:Clone()
    Wrecker.Parent = workspace
    Wrecker:SetPrimaryPartCFrame(Wrecker.PrimaryPart.CFrame * CFrame.new(math.random(workspace.G1.CFrame.X, workspace.G2.CFrame.X, workspace.G3.CFrame.X, workspace.G4.CFrame.X, workspace.G5.CFrame.X, workspace.G6.CFrame.X,workspace.G7.CFrame.X, workspace.G8.CFrame.X),0,0))
end

Its a default script in the ServerScriptService

2 answers

Log in to vote
2
Answered by 8 years ago

You're using math.random wrong for what you want to do. It only accepts a maximum of two arguments but you're giving it way more than two arguments.

I would suggest using string concatenation with math.random to choose a part with G at the start with any random number between one and eight and then continue from there.

Also, math.random will only return a random integer, which means that no decimals will be returned, only whole numbers.


while wait(math.random(3)) do --Math random only returns integers and won't return decimals, only whole numbers.
    local Wrecker = game.ServerStorage.Wrecking:Clone()
    Wrecker.Parent = workspace
    local GPart = workspace["G"..math.random(8)] --Choose a random G part with a random number between 1 and 8.
    Wrecker:SetPrimaryPartCFrame(Wrecker.PrimaryPart.CFrame * CFrame.new(GPart.CFrame.X,0,0)) --Use the X property of GPart's CFrame.
end

I hope my answer helped you. If it did, be sure to accept it.

Ad
Log in to vote
3
Answered by
KoreanBBQ 301 Moderation Voter
8 years ago

math.random only takes two arguments, min and max, and will only return an integer.

If you wish to get a decimal, you'll have to divide a random integer, like so:

local dec=math.random(5,30)/10
wait(dec)

Also you have to fit all those G#Number inside two integers. You could either store them in a table or do:

workspace["G"..math.random(1,8)
0
Nice answer but since it is already answered I can't accept thanks anyway. Hero_ic 502 — 8y

Answer this question