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

How can I fix my random model spawner?

Asked by 7 years ago

Ok so, my script doesn't seem to be working. I'd like it so that the script would randomly pick a model and spawn it onto a specific block but its not working at all.. :/

Heres my script:

--By TYMZ4ACTION

RespawnTime = 5

function spawn()
math.randomseed(tick)
--This makes sure the spawning is completely random.
tools = game.ServerStorage.Folder:GetChildren()
-- This function creates a table containing all your tools.
clone = tools[math.random(1,#tools)]:Clone()
-- This picks a random tool out of the possibilities.
clone.Position = script.Parent.Position
-- This makes sure it spawns in the same position as the spawn block...
clone.Parent = script.Parent
-- ... And this places it into your world
clone.Name = "Pickup"
clone.AncestryChanged:connect(function() wait(RespawnTime) spawn())
end

Any help will be greatly appreciated! :) -TYMZ4ACTION

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
7 years ago
Edited 7 years ago

Your problem

You're attempting to index a property that is nil for tools: Position. What you can do is reference the handle of the tool, and manipulate the Position of the handle.

Also, you never closed your anonymous function on line 17.

Sidenotes

  • Since you told me in the chat that game.ServerStorage.Folder is never manipulated, it is ok to define it on the outside of your function. This is more efficient because then the variable is not being defined everytime the spawn function fires.

  • You should change the name of your function, since 'spawn' is already a core function in RBX.lua - 'SpawnTool' should suffice.

  • Notes should come before the line of code you are referring to.

  • This is new, and even I forget sometimes, but connect is deprecated. Use Connect.

  • And lastly, remember to tab your code. It keeps everything nice and legible.

Code

local RespawnTime = 5
local tools = game.ServerStorage.Folder:GetChildren()

function SpawnTool()
    local clone = tools[math.random(1,#tools)]:Clone()
    clone.Handle.Position = script.Parent.Position
    clone.Parent = script.Parent
    clone.Name = "Pickup"
    clone.AncestryChanged:Connect(function() 
        wait(RespawnTime) 
        SpawnTool() 
    end)
end

 --You have to call it the first time for the feedback loop to begin.
SpawnTool()

Hope I helped! And happy developing!

Ad

Answer this question