0

I tried to make an item spawn every 60 seconds and in one of two locations, but when I tried to use vector3 it didn't spawn in the location of the "spawnplace1" or "spawnplace2", but it spawned ontop of the roka being copied and didn't move. Also I don't think I can hold the copied roka's either. Here's the code!

local roka = workspace["Rokakaka Fruit"]
local itemspawns = workspace.ItemSpawnLocals
local itemspawn1 = itemspawns["Item Spawn 1"]
local itemspawn2 = itemspawns["Item Spawn 2"]

local place1 = itemspawn1.Position
local place2 = itemspawn2.Position

wait(60)
local spawnplace1 = math.random(1,2)
local spawnplace2 = math.random(1,2)

if spawnplace1 == 1 then
    roka2 = roka:Clone()
    roka2.Parent = workspace
    
    local roka2handle = roka2.Handle
    
    roka2handle.Position = Vector3.new(itemspawn1)

elseif spawnplace1 == 2 then
    roka2 = roka:Clone()
    roka2.Parent = workspace

    local roka2handle = roka2.Handle

    roka2handle.Position = Vector3.new(itemspawn2)
end

print(spawnplace1)
print(spawnplace2)
Vlo_tz
  • 21
  • 4

1 Answers1

1

The Vector3 holds coordinates of a point in 3D space. You have only provided 1 of 3 pieces of information in the constructor. To construct a Vector3, you need to provide the Y and Z axes as well, like this :

roka2handle.Position = Vector3.new(1, 2, 3)

But you don't need to explicitly create a Vector3 to get your code working. You can just assign the positions of the spawn locations to your newly created fruit, and that should do the trick. This way, you can add lots more spawn locations, and you don't need to update the script each time.

-- grab some things from the workspace
local roka = workspace["Rokakaka Fruit"]
local itemSpawns = workspace.ItemSpawnLocals

-- choose a random spawn location
local spawnLocations = itemSpawns:GetChildren()
local spawnNumber = math.random(1, #spawnLocations)
local spawnPosition = spawnLocations[spawnNumber].Position

-- spawn and move a new fruit to one of the spawn locations
roka2 = roka:Clone()
roka2.Parent = workspace
local roka2handle = roka2.Handle
roka2handle.Position = spawnPosition

-- debug
print("spawning fruit at : ", spawnPosition)

As a side note, if roka2 is a Model, you may want to consider using roka2:SetPrimaryPartCFrame( CFrame.new(spawnPosition)) to move it around.

Kylaaa
  • 6,349
  • 2
  • 16
  • 27