Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Spawning Tools at Random on a Part?

I am looking for an easier way to spawn my tools on the map. Here is my script so far:

while true do
	local values = game.ReplicatedStorage.Spawns:GetChildren()
	local value = values[math.random(1,#values)]
	local parts = game.ServerStorage.MushroomFolder:GetChildren()
	local part = parts[math.random(1,#parts)]:Clone()
	part.Handle.Position = value.Value
	part.Parent = workspace.tools
	wait(10)
end

As you can see I have to make loads of different Vector3 values and pick between them.

Could anyone show me a better way to do this? Thanks in advance!

1 answer


To position your items, you would use vector3.new. Instead of having a value for the position, we need a value of each x, y, and z. Inside of your Mushroomfolder, instead of having stringvalues, or whatever else you have, have multiple folders in the mushroomfolder named spawn1, spawn2, spawn3, etc. Inside each of these folders insert three int values. Name them each x, y, and z. Add the corresponding x, y, and z of your position to each value.

local rs = game:GetService("ReplicatedStorage")
local ss = game:GetService("ServerStorage")
while true do
    local values = rs.Spawns:GetChildren()
    local value = values[math.random(1,#values)]
    local parts = ss.MushroomFolder:GetChildren()
    local part = parts[math.random(1,#parts)]:Clone()
	local valuex = value.x.Value --the x value
	local valuey = value.y.Value --the y value
	local valuez = value.z.Value --the z value
    part.Handle.Position = Vector3.new(valuex,valuey,valuez) --places the tool to the values
    part.Parent = workspace.tools
    wait(10)
end

This would put the value in the x into the x value for the vector 3, and so on.

Edited:

We would probably use math.random

local ss = game:GetService("ServerStorage")
while true do
    local parts = ss.MushroomFolder:GetChildren()
    local part = parts[math.random(1,#parts)]:Clone()
	local valuex = math.random(-100,100) --the x value. Change -100 and 100 to the minimum and maximum for the position
	local valuey = math.random(-100,100) --the y value. Change -100 and 100 to the minimum and maximum for the position
	local valuez = math.random(-100,100) --the z value. Change -100 and 100 to the minimum and maximum for the position 
    part.Handle.Position = Vector3.new(valuex,valuey,valuez) --places the tool to the values
    part.Parent = workspace.tools
    wait(10)
end

2 comments

0

But is there any way to generate a completely random position on the map?

0

Try it now

This discussion is preserved as it was. New answers, comments, and votes are closed.