My map clone script is not working

Hello my script is not working and its suppose to clone a map in the game and its using a module script and a regular script to connect with each other here are both parts of the script that are not working.

–Module script
function module.SelectChapter()

local rand = Random.new()

local chapters = game.ReplicatedStorage.Chapters:GetChildren() --Table of all maps in the game available

local chosenChapter = chapters[rand:NextInteger(1,#chapters)]

return chosenChapter

end
–Regular Script
local chosenChapter = Round.SelectChapter()

local clonedChapter = chosenChapter:Clone()

clonedChapter.Name = “Map”

clonedChapter.Parent = game.Workspace

1 Like

You shouldn’t really use a module script I would just do.

–RegularScript

local availableMaps = game.ReplicatedStorage.Chapters:GetChildren() -- I recommend putting them in server storage, not replicated storage.
local selectedMap = math.random(1, #availableMaps)

local clonedMap = availableMaps[selectedMap]:Clone()
clonedMap.Parent = workspace -- use this instead of game.Workspace

use math.random instead of next integer

function module.SelectChapter()



local chapters = game.ReplicatedStorage.Chapters:GetChildren() --Table of all maps in the game available

local chosenChapter = chapters[math.random(1,#chapters)]

return chosenChapter

end 

other code

local Round= require()

local chosenChapter = Round.SelectChapter()

local clonedChapter = chosenChapter:Clone()

clonedChapter.Name = “Map”

clonedChapter.Parent = workspace
2 Likes

Ok so when I put the code in it said in my output box attempted to call require with invalid arguments. And here is the line that caused it.

local Round = require()

Hello there is nothing to require in the script what should it require? Here is the line of code that is causing the problem.

local Round = require()

Using a module script is useless if you can just do it fully on the server

I recognise this code. It’s very important that you follow the steps in the tutorial, otherwise issues like this can occur. As for when you post threads to this category, please debug first then post a thread if you can’t figure it out after consulting the tutorial again - with the exact error.

@iiFusionzX Yes you should use a ModuleScript where possible. They aren’t useless and are able to be used on both environments. They are good as reusable pieces you can transfer anywhere, be it a library or a container of data/constants. As for OP’s specific case, it’s from one of Alvin_Blox’s tutorials, how to create a Piggy game.

@FerbZides math.random and Random.new run on the same algorithm. NextInteger is not the problem. If you don’t care for all the benefits the Random object provides then you can still use math.random, otherwise this suggestion doesn’t change what the code is doing fundamentally.

1 Like

Ok I will be sure to do that next time!