I've been trying to make a script where where, every so often, a crystal will spawn in the bounds of the map. I have a while wait()
loop in a script. The script is in ServerScriptService. The error is coming from the loop and says: "Requested Module is Required Recursively".
My code:
local Rate = script.resourceRate local MaxSpawn = Vector3.new(218.44, 0.5, -227.733) local MinSpawn = Vector3.new(-228.008, 0.5, 223.37) print("Localled") while wait(math.exp(Rate.Value))do print("Making") local Crystal = game.ServerStorage.Crystal:Clone() Crystal.Parent = workspace Crystal.CFrame = math.random(Vector3.new(218.44, 0.5, -227.733), Vector3.new(-228.008, 0.5, 223.37)) end
Anybody have an idea on how to fix this? It's never happened before.
The error is not coming from your code: it's pretty obvious that you aren't requiring any module script, let alone recursively requiring a module within the module itself. The error is most likely thrown by roblox's built-in building tools
There is, however, an issue with your code, most likely unseen because of the output being spammed by the same error message from roblox's built-in building tools: math.random
can only accept numbers and it'll error otherwise. Here's what your solution would look like:
local Rate = script.resourceRate local MaxSpawn = Vector3.new(218.44, 0.5, -227.733) local MinSpawn = Vector3.new(-228.008, 0.5, 223.37) print("Localled") -- Return a random floating point number (decimal number) since math.random doesn't accept nor return floats function randomFloat(n, m) return math.random() * (m - n) + n end while wait(math.exp(Rate.Value))do print("Making") local Crystal = game.ServerStorage.Crystal:Clone() Crystal.Parent = workspace Crystal.CFrame = CFrame.new( randomFloat(MinSpawn.X, MaxSpawn.X), 0.5, randomFloat(MaxSpawn.Z, MinSpawn.Z) ) end
Use a variable in the script. Instead of using an instance to hold that value, you can use variables to hold that value. Plus, It gives less memory of the file. Also, you can just do this:
local Rate = -- your value -- (number) local MaxSpawn = Vector3.new(218.44, 0.5, -227.733) local MinSpawn = Vector3.new(-228.008, 0.5, 223.37) print("Localled") while wait(math.exp(Rate))do print("Making") local Crystal = game.ServerStorage.Crystal:Clone() Crystal.Parent = workspace Crystal.CFrame = math.random(Vector3.new(218.44, 0.5, -227.733), Vector3.new(-228.008, 0.5, 223.37)) end
I hope that helped.