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

Spawning part in a specific range gone wrong?! (Part 2)

Asked by 5 years ago
Edited 5 years ago

This is just an addition to part 1 which you could find over here: https://scriptinghelpers.org/questions/78764/spawning-a-part-in-random-positions-gone-wrong-updated

(you dont' need to read part 1 to understand this)

Basically, I wanted to spawn parts in a specific region. However, the parts only spawned in the Vector3 Positions I gave in the script. The computer did not generate warnings or errors.I wanted to spawn the piece, however in between those positions or in a region between those positions. This is my code:

local  GrassIsCutted =  false -- ignore this. its for a different purpose

local  OriginalPiece =  game.ServerStorage.OriginalPart


local  positionsTable =  {Vector3.new(-59.5,1.5, -23.5), Vector3.new(-59.5,1.5,26.5),Vector3.new(-8.23,1.5,26.467),Vector3.new(-8.5,1.5, -23.5)}

function  duplicatePiece()

math.randomseed(tick())
local  position =  positionsTable[math.random(1,#positionsTable)]

local  clonedPiece =  OriginalPiece:Clone()`

clonedPiece.Position =  position

clonedPiece.Parent =  game.Workspace`

end



while  true  do
wait(10)
duplicatePiece()
end

Please Help! Thank you for taking your time to read and debug this and have a wonderful day. any answers will hopefully be UpVoted by the community.

1 answer

Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

Sorry, I misinterpreted the use of the Vector3 Table, I thought you were trying to randomly place parts in one/four of those positions, but I wasn't reading carefully enough. I figured out a solution to it, though.

I modeled out where these positions would be using parts, and I see it models out to be a square shape. I was wondering, maybe you could have an invisible part that could act as the region in which parts would randomly be positioned in.

Here's a photo showing what I mean The white nodes represent the positions in the table, and the red part is the region.

Using this process, here's the code that I came up with:

local GrassIsCutted = false -- ignore this. its for a different purpose
local OriginalPiece = game.ServerStorage.OriginalPart

local region =  game.Workspace:WaitForChild("Region")
local size = region.Size

local function neg(number)
    if math.random(1, 2) == 1 then
       return number
    else
        return -(number)
    end
end
 --Randomizes whether the number can be negative or not

function duplicatePiece()

    math.randomseed(tick())

    local clonedPiece = OriginalPiece:Clone()
    clonedPiece.Parent = game.Workspace

    clonedPiece.CFrame = region.CFrame
    --Centers the piece to the center of the region before operating

    local x = size.X/2
    local z = size.Z/2
    --X-Axis and Z-Axis of the region. Y is excluded because that's the height
    --Refer to Explanation 1

    clonedPiece.CFrame = clonedPiece.CFrame * CFrame.new(neg(math.random(1, x*10000)/10000), 0, neg(math.random(1, z*10000)/10000))
    --Randomizes the CFrame of the Part in the X-axis and Z-Axis
    --Refer to Explanation 2
end

while wait(10) do
    duplicatePiece()
end

Here's what the end result would look like

This code will work with any assigned region. Just change the 4th line to the location of the region.

Explanation 1 I'm only using the X and Z axes because Y is not affected. Since Y represents the height of the region, it is not altered or affected, just the back-forward and left-right movement of the part.

The reason why I'm dividing each size by 2 is because I'm varying the position of the part relative to the center of the region. This may sound confusing, so I drew a little diagram to show you what I mean.

Here's the diagram

Explanation 2 In this section, I am taking the x/2 and z/2 values and multiplying it by 10000. The reason is because numbers can go up to the .0001 significant figure when divided by two, depending on the size. math.random functions wouldn't work with decimals, so I changed these to a whole number by multiplying them by 10000 and afterwards, when a whole number is reached, divide it back by 10000 to get the exact decimal.

I used the neg() function to return the number to either negative or positive. Negative values either move the part left or backwards, and positive values move then right or forwards.

I really hope I answered your question and helped you through this process :)

0
Thank you! This very detailed explanation really helped! Nice skills! VVickedDev 54 — 5y
Ad

Answer this question