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

How do i make this tool spawn randomly in a radius around the spawner?

Asked by 2 years ago

I tried to make a spawner that spawns a tool (a pink cube) randomly in a radius around the spawner but the cube doesnt move and doesnt spawn randomly

--// Variables
local cframeX = workspace.testSpawner.Position.X
local cframeY = workspace.testSpawner.Position.Y
local cframeZ = workspace.testSpawner.Position.Z
local randomspawn = Vector3(math.random(cframeX - 20, cframeX + 20), cframeY + 20, math.random(cframeZ -20, cframeZ + 20))
local randomspawntimer = math.random(5, 8)
local numb = 0
local canSpawn = true
local Workspace = game.Workspace
local PinkCube = Workspace:WaitForChild('PinkCube')
local PinkSpawner = workspace.testSpawner

--// Main
while true do 
    if canSpawn then
        if numb <= 25 then
            canSpawn = false
    local PCC = PinkCube:Clone()
    PCC.Parent = PinkSpawner
        PCC.Handle.Position = Vector3(math.random(cframeX - 20, cframeX + 20), cframeY + 20, math.random(cframeZ -20, cframeZ + 20))
        wait(randomspawntimer)  
        canSpawn = true
        numb = numb + 1
        end
    end


    end



end

1 answer

Log in to vote
0
Answered by 2 years ago

You actually have the script working properly!

You just missed the .new on your Vector3 values. (Change them to Vector3.new(x, y, z) ) And then you have one too many ends in there.

The way to fix that is just to remove your second to last "end" from the while loop.

The fixed code looks like this if you want to just copy paste it.

--// Variables
local PinkSpawner = workspace:FindFirstChild("testSpawner")
local cframeX = PinkSpawner.Position.X
local cframeY = PinkSpawner.Position.Y
local cframeZ = PinkSpawner.Position.Z
local randomspawn = Vector3.new(math.random(cframeX - 20, cframeX + 20), cframeY + 20, math.random(cframeZ -20, cframeZ + 20))
local randomspawntimer = math.random(5, 8)
local numb = 0
local canSpawn = true
local PinkCube = workspace:WaitForChild('PinkCube')

--// Main
while true do 
    if canSpawn then
        if numb <= 25 then
            canSpawn = false
            local PCC = PinkCube:Clone()
            PCC.Parent = PinkSpawner
            PCC.Handle.Position = Vector3.new(math.random(cframeX - 20, cframeX + 20), cframeY + 20, math.random(cframeZ -20, cframeZ + 20))
            wait(randomspawntimer)  
            canSpawn = true
            numb = numb + 1
        end
    end
end

(I also removed some of the unnecessary code. (i.e., the workspace.testSpawner.Position.X,Y,Z) )

If this helped, don't forget to mark this as the answer so people know that it's solved.

If you have any other questions, or questions about anything above feel free to ask!

0
There are so much wasted lines here Kingu_Criminal 205 — 2y
0
I just copied his code and fixed what was wrong. It wasn't trying to remove the lines or optimize his code. MrLonely1221 701 — 2y
0
I wasn't* MrLonely1221 701 — 2y
Ad

Answer this question