generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

[Random Object Spawning] ~ Help?

Previous Thread :: Next Thread 
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 05:52 PM
Recently, I made a script that clones an object/'Part' called "Key" from ServerStorage, and spawns 10 of its clones randomly on 15 dedicated spawn points, on button touch. I found a bug. Sometimes, 2 keys spawned on a point. I would like to edit the script as such that 10 keys, and only one key spawns at a spawn point at one time, and a total of 10 points are used out of 15. Help?

function TouchStart(hit, Table, f)

local n = 0

repeat
wait()
local keyZ = game.ServerStorage.Key
local keyclone = keyZ:Clone()
keyclone.Parent = game.Workspace
print("Key has been spawned!")

Table = { Vector3.new( 1, 2.5, -3 ), Vector3.new( 10, 2.5, -3), Vector3.new( 20, 2.5, -3 ) } -- ONLY 3 POINTS LISTED OUT OF 15...
f = Table[math.random(1,#Table)]
keyclone.Position = f

n = n + 1

until n == 10
script.Disabled = true
wait(5)

end

script.Parent.Touched:connect(TouchStart)
Report Abuse
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 05:53 PM
Correction: ...as such that 10 keys spawn*
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
25 Jun 2015 05:55 PM
function SpawnParts(parts, pos)
local pos2 = {unpack(pos)}
for i = 1,#parts do
parts[i].Position = table.remove(pos2, math.random(#parts))
end
return pos2
end
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
25 Jun 2015 05:57 PM
Usage:

SpawnParts({part1, part2, part3 --[[List of parts]]}, {Vector3.new(1, 1, 1), Vector3.new(2, 2, 2) --[[List of positions]]})
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
25 Jun 2015 05:58 PM
>until n == 10

You're stopping the loop on the 10th iteration, before the last 5 are used.
Report Abuse
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 06:02 PM
The spawn parts here are the points, or the objects to be spawned? Not really familiar with this method, but I'll give this a try.
Report Abuse
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 06:03 PM
@Widths: Exactly. I want only 10 keys to spawn, and on 10 out of 15 spawn points separately. Currently, more than one key can randomly spawn per point. So that's the issue.
Report Abuse
Widths is not online. Widths
Joined: 12 Aug 2014
Total Posts: 41286
25 Jun 2015 06:04 PM
Oh sorry I did not understand the question
Report Abuse
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 06:06 PM
Yeah. Made a mess right there.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
25 Jun 2015 06:12 PM
function SpawnParts(part, num, pos)
local pos2 = {unpack(pos)}
for i = 1,num do
local p = part:Clone()
p.Parent = workspace
p.Position = table.remove(pos2, math.random(#parts))
end
return pos2
end


Usage:

SpawnParts(game.ServerStorage.Key, 10, {
--list the 15 positions here
})
Report Abuse
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 06:25 PM
@warspyking: Tested, but it spawns a single key in the center of the map. Can't tell what's wrong. No errors in the output.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
25 Jun 2015 06:30 PM
One of your positions must be the map centre then :P
Report Abuse
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 06:37 PM
Lol, then I'd be missing 9 keys still. Trying a test with 3 positions.

function SpawnParts(part, num, pos)
local pos2 = {unpack(pos)}
for i = 1,num do
local p = part:Clone()
p.Parent = game.Workspace
p.Position = table.remove(pos2, math.random(#part))
end
return pos2
end

local key = game.ServerStorage.Key
SpawnParts(key, 10, {Vector3.new(1,2.5,-3),Vector3.new(10,2.5,-3),Vector3.new(20,2.5,-3)})

Output:-
04:35:11.771 - Workspace.RoundStart:6: attempt to get length of local 'part' (a userdata value)
04:35:11.772 - Stack Begin
04:35:11.774 - Script 'Workspace.RoundStart', Line 6 - global SpawnParts
04:35:11.774 - Script 'Workspace.RoundStart', Line 12
04:35:11.775 - Stack End
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
25 Jun 2015 06:41 PM
Sorry, forgot to change something in my original function. Fixed:

function SpawnParts(part, num, pos)
local pos2 = {unpack(pos)}
for i = 1,num do
local p = part:Clone()
p.Parent = game.Workspace
p.Position = table.remove(pos2, math.random(num))
end
return pos2
end

local key = game.ServerStorage.Key
SpawnParts(key, 2, {Vector3.new(1,2.5,-3),Vector3.new(10,2.5,-3),Vector3.new(20,2.5,-3)})




There has to be more (or the same) positions then parts, because the script make sure 2 parts are never spawned in the same place, so make sure there's enough positions for the parts 😄
Report Abuse
zedix123 is not online. zedix123
Joined: 29 Mar 2012
Total Posts: 91
25 Jun 2015 06:44 PM
Ohh. I got it. Thanks a lot! Works like charm.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
25 Jun 2015 06:46 PM
Np, glad I could help 😄
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image