Hi, I want to make the thing the title suggests. Except I can’t figure out how I can do this. I keep getting the error:
invalid argument #2 to ‘random’ (number expected, got Vector3).
I have seen no posts on this. Can anyone help? Here is my current code:
(I kind of butchered a freemodel code that randomizes part spawning)
local spawnArea = script.Parent -- Script should be inside the part that marks the area
game.Players.PlayerAdded:Connect(function(plr)
while wait(.1) do
local clone = Instance.new("Part") -- Part you want to clone
clone.Parent = workspace -- Add it to the game
clone.Anchored = true
clone.Name = "Map"
local color = BrickColor.random()
local size = Vector3.new(math.random(1,30),math.random(1,30),math.random(1,30))
local posX = math.random(math.random(1,10),plr.Character:WaitForChild("HumanoidRootPart").Position) -- I get the error here and probably PosZ too because its the same as this
local posY = plr.Character:WaitForChild("HumanoidRootPart").Position + Vector3.new(0,50,0)
local posZ = math.random(math.random(1,10),plr.Character:WaitForChild("HumanoidRootPart").Position) -- Gets the random Z value
local pos = Vector3.new(posX,posY,posZ) -- Marks the final vector
clone.BrickColor = color
clone.Size = size
clone.CFrame = (spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea
end
end)
Lmk if you have any questions!
Hopefully this works for you :3
local spawnArea = script.Parent -- Script should be inside the part that marks the area
game.Players.PlayerAdded:Connect(function(plr)
if plr then
while wait(.1) do
local clone = Instance.new("Part") -- Part you want to clone
clone.Parent = workspace -- Add it to the game
clone.Anchored = true
clone.Name = "Map"
local humrootpart = plr.Character:WaitForChild("HumanoidRootPart")
local color = BrickColor.random()
local size = Vector3.new(math.random(1,30),math.random(1,30),math.random(1,30))
local posX = Vector3.new(math.random(1, 10), humrootpart.Position)
local posY = plr.Character:WaitForChild("HumanoidRootPart").Position + Vector3.new(0, 50, 0)
local posZ = Vector3.new(math.random(1, 10), humrootpart.Position)
local pos = Vector3.new(posX, posY, posZ) -- Marks the final vector
clone.BrickColor = color
clone.Size = size
clone.CFrame = (spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea
end
end
end)
Greetings! I believe I have found a solution to your issue. For your script, you’re trying to randomize 4 numbers into one value. Vector3 can only have 3 values, being X,Y,Z. When using “plr.Character.HumanoidRootPart.Position”, you’re getting the entire position value, not the X, Y, or Z individually. To add onto @riddleminecart’s script, you’re trying to create an entire position for each pos#. You also want to use a characteradded event to make sure the character exists.
Long story short, you just need one number in the end per position value.
local spawnArea = script.Parent -- Script should be inside the part that marks the area
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
while wait(.1) do
local clone = Instance.new("Part") -- Part you want to clone
clone.Parent = workspace -- Add it to the game
clone.Anchored = true
clone.Name = "Map"
clone.CanCollide = false
local humrootpart = char:WaitForChild("HumanoidRootPart")
local color = BrickColor.random()
local size = Vector3.new(math.random(1,30),math.random(1,30),math.random(1,30))
local posX = math.random(1, 10) + humrootpart.Position.X -- generates random X position using the X position on hrp.
local posY = char:WaitForChild("HumanoidRootPart").Position.Y + 50 -- sets the Y position to 50 using the Y position on hrp.
local posZ = math.random(1, 10) + humrootpart.Position.Z -- generates a random Z position using the Z position on hrp.
local pos = Vector3.new(posX, posY, posZ) -- Marks the final vector
clone.BrickColor = color
clone.Size = size
clone.CFrame = (spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea
end
end)
end)
Hello! Your code does not work. The blocks spawn way above 50 studs above the player, and second, it is not randomizing the way I want it to. I want it to space out, here is a picture of whats happening right now.
(excuse my terrible handwriting. its hard to write on a mouse)
I am really not sure what effect you are wanting to do, I tried to clean the code a bit.
At the moment, I don’t know where you are wanting these random parts to spawn, because you are using the player’s hrp position, but also the spawn point cframe.
However this code has no errors, and it is generating a posX and posZ by getting a random ± 1-10
local spawnArea = script.Parent -- Script should be inside the part that marks the area
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(character)
local hrp = character:WaitForChild("HumanoidRootPart")
while wait(.1) do
local clone = Instance.new("Part") -- Part you want to clone
clone.Anchored = true
clone.Parent = workspace -- Add it to the game
clone.Name = "Map"
local color = BrickColor.random()
local size = Vector3.new(math.random(1,30),math.random(1,30),math.random(1,30))
local posX = (math.random(0,20)-10) + hrp.Position.X -- I get the error here and probably PosZ too because its the same as this
local posY = hrp.Position.Y + 50
local posZ = (math.random(0,20)-10) + hrp.Position.Z -- Gets the random Z value
local pos = Vector3.new(posX,posY,posZ) -- Marks the final vector
clone.BrickColor = color
clone.Size = size
clone.CFrame = (spawnArea.CFrame * CFrame.new(pos)) -- Positions the part inside the spawnArea
game.Debris:AddItem(clone,5) --removes the part after 5 seconds
end
end)
end)
I want random objects spawned near the player but at a fixed height. So basically the objects spawn around the player whenever the player travels around the map and unload the objects that are too far away for optimization. Right now I am only focusing on the first part, but I will need help on the second part aswell.