My module script works perfectly fine the first time, but once it’s ran again, it stops working.
Code
local module = {}
local PS = game:GetService('Players')
local ContentProvider = game:GetService('ContentProvider')
local RPS = game:GetService('ReplicatedStorage')
local Maps = RPS:WaitForChild('Maps', 10)
local children = Maps:GetChildren()
local CharacterHolder = RPS:WaitForChild('CharacterHolder', 10)
local workspace = game:GetService('Workspace')
local currentMapFolder = workspace:WaitForChild('CurrentMap', 10)
local lastMap = nil
local chars = {}
local spawns = {}
local function chooseRandomMap(chooseMapTime: number, choosingMapValue: BoolValue)
local chosenMap = children[math.random(1, #children)]:Clone()
if chosenMap ~= lastMap then
lastMap = chosenMap
if choosingMapValue and choosingMapValue:IsA('ValueBase') then
choosingMapValue.Value = true
print('Setting value to true')
end
for _, spawn in pairs(chosenMap:GetDescendants()) do
if spawn:IsA('SpawnLocation') then
table.insert(spawns, spawn)
end
end
for _, plr in pairs(PS:GetPlayers()) do
if plr:IsA('Player') then
local char = plr.Character or plr.CharacterAdded:Wait()
char.Parent = CharacterHolder
table.insert(chars, char)
end
end
task.wait(chooseMapTime)
chosenMap.Parent = currentMapFolder
print('test 1')
for _, char:Model in pairs(chars) do
local randomSpawn = spawns[math.random(1, #spawns)]
ContentProvider:PreloadAsync({randomSpawn})
char.Parent = game:GetService('Workspace')
char:MoveTo(randomSpawn.Position + Vector3.new(0, char:GetExtentsSize().Y/2, 0))
end
print('test 2')
if choosingMapValue and choosingMapValue:IsA('BoolValue') then
print('Setting value to false')
choosingMapValue.Value = false
end
else
chooseRandomMap(chooseMapTime, choosingMapValue)
end
end
function module:ChooseRandomMap(chooseMapTime: number, choosingMapValue: BoolValue)
chars = {}
spawns = {}
if #currentMapFolder:GetChildren() > 0 then
for _, map:Instance in pairs(currentMapFolder:GetChildren()) do
if map:IsA('Model') then
map:Destroy()
end
end
end
chooseRandomMap(chooseMapTime, choosingMapValue)
end
return module
It fires all the test prints, but once I try to set the “choosingMapValue” value at the end, it doesn’t recognize it as a BoolValue, or as anything (even though I have it as one)
Hi, I can’t find an issue with the code posted. The only thing I noted was the first check was for choosingMapValue:IsA('ValueBase') which theoretically could allow it to be the wrong instance value type. But I would have expected an error when setting the value, and the type checker should have highlighted it, so really don’t think that is it.
So, as it works first time around, would there be any other script which could destroy/interfere with it during the choosingMapTime wait?
I fixed a few things with your script, and I just want to see if this works.
Code:
local module = {}
local PS = game:GetService('Players')
local ContentProvider = game:GetService('ContentProvider')
local RPS = game:GetService('ReplicatedStorage')
local Maps = RPS:WaitForChild('Maps', 10)
local children = Maps:GetChildren()
local CharacterHolder = RPS:WaitForChild('CharacterHolder', 10)
local currentMapFolder = workspace:WaitForChild('CurrentMap', 10)
local lastMap = nil
local chars = {}
local spawns = {}
local function chooseRandomMap(chooseMapTime: number, choosingMapValue: BoolValue)
if not choosingMapValue or not chooseMapTime then
warn("Missing parameters")
return
end
if typeof(choosingMapValue) ~= "Instance" or not choosingMapValue:IsA("BoolValue") then
warn("Invalid choosingMapValue")
return
end
local chosenMap = children[math.random(#children)]
if chosenMap ~= lastMap then
lastMap = chosenMap
chosenMap = chosenMap:Clone()
choosingMapValue.Value = true
print('Setting value to true')
for _, spawn in chosenMap:GetDescendants() do
if spawn:IsA('SpawnLocation') then
table.insert(spawns, spawn)
end
end
for _, plr in PS:GetPlayers() do
local char = plr.Character or plr.CharacterAdded:Wait()
char.Parent = CharacterHolder
table.insert(chars, char)
end
task.wait(chooseMapTime)
chosenMap.Parent = currentMapFolder
print('test 1')
for _, char: Model in chars do
local randomSpawn = spawns[math.random(#spawns)]
ContentProvider:PreloadAsync({randomSpawn})
char:MoveTo(randomSpawn.Position + Vector3.new(0, char:GetExtentsSize().Y/2, 0))
char.Parent = workspace
end
print('test 2')
print('Setting value to false')
choosingMapValue.Value = false
else
chooseRandomMap(chooseMapTime, choosingMapValue)
end
end
function module:ChooseRandomMap(chooseMapTime: number, choosingMapValue: BoolValue)
table.clear(chars)
table.clear(spawns)
for _, map: Instance in currentMapFolder:GetChildren() do
if map:IsA('Model') then
map:Destroy()
end
end
chooseRandomMap(chooseMapTime, choosingMapValue)
end
return module
Things I fixed:
table.clear is more performant than doing table = {}
pairs and ipairs are slower than the newly added generalized iteration Roblox provides
Moved checks to the top of the function so you don’t have to check later
Your chosenMap ~= lastMap check would have failed because even if the exact same map was chosen, because you cloned it before the check, it would recognize it as a totally different map
Instead of math.random(1, value), you can just do math.random(value)
:GetPlayers will only ever return a Player, so the check is unnecessary
I know noticed on the “task.wait(chooseMapTime)”, when I remove it, the script works fine indefinitely. But when it’s there it doesn’t work; do you have any ideas why?
And thinks for the tips. I was completely unaware that those were added/were a thing.