Problems with my game

Hello fellow scripters,

Today I have 2 problems that I hope you all can help me fix, I will give you an introduction to my game so you have a better idea of what I’m trying to achieve, my game is a round based obby game, every round a random obby gets chosen and put into the Workspace then players get teleported to the Obby and they will compete to reach the end of the obby with a timer and whoever reaches the end basically wins.

Problem #1:

Alright so for the first problem, I’m trying to select a random map from my maps, it works fine until somehow the map’s parent gets set to nil a few milliseconds after setting it’s parent to Workspace and I have done nothing for that to happen so I don’t know how this is possible at all, can someone tell me what is happening? Here’s what I’m using:

Problem #1 was fixed by me

Problem #2:

For the second problem, well I’m getting an error when trying to iterate throught the “players” table which contain the players currently playing the obby, the error says “attempt to call a nil value” but I don’t know why the “players” table is considered a nil value, here’s what I’m using to do all of this:

Problem #2 was fixed by @lordmochibi

1 Like

for the 2nd one when you’re looking for player in workspace you need to search by the player’s name. currently

local Player = workspace:FindFirstChild(v)

you’re searching for the object and not the name of the player so you would just change v to v.Name assuming that players above this is defined in getchildren.

As for your first problem I don’t see why it is printing nil unless something is being done in the other module functions.

1 Like

Even if this would do anything the ModuleScript doesn’t even get to that point because it errors when I try to get the players in the players table, read my post about this again for more information.

There’s no functions in my ModuleScript doing anything to the obby map’s parent.

Any other solutions?

well for getting the players I can see you try to set the players at the top of your server script and there is no function calling them. Try to create/insert players into a table when you’re creating the game for the next round. Currently it’s just creating a player list at the very top of your script which will run before a player even loads in and will never update on player’s added.

I’m not too sure about the first problem though I’ll try and replicate it.

1 Like

Try this:
If you want to implement multiple rounds try using loops.

--Module script
local GameState = script.Status
local module = {}

function module.ChooseObby(Obbies) -- A table with name as indexes and hobbies as values.
	local CurrentObby = Obbies[math.random(1,#Obbies)]
	
	GameState.Value = tostring(CurrentObby.Name).." has been chosen!"
	wait(2)
	
	return CurrentObby
end

function module.TeleportToObby(players,map)
	assert(players,"Need plrs")
	assert(players,"Need a map")
	
	local Spawns = {}
	
	for i,v in ipairs(map:GetChildren()) do
		if v.Name == "Spawn" then
			table.insert(Spawns,v)
		end
	end
	if Spawns[1] then
		for plr, _ in pairs(players) do
			if plr.Character then
				plr.Character.HumanoidRootPart.Position = Spawns[math.random(1,#Spawns)].Position + Vector3.new(0,3,0)
			end
		end
	else
		warn("Did not find any spawns!")
	end
end

return module

--Server script
local players = game:GetService("Players")

local Contestants = {} --Table of players that are in the mini game.

players.PlayerAdded:Connect(function(plr)
	
	repeat wait(1) until plr:HasAppearanceLoaded() == true or plr.Character
	
	Contestants[plr] = true
end)

players.PlayerRemoving:Connect(function(plr)
	Contestants[plr] = nil
end)

local ObbyModule = require(script.Parent.ModuleScript) --The module

local CurrentObby = ObbyModule.ChooseObby(game:GetService("ReplicatedStorage").Obbies:GetChildren())

local GameMap = CurrentObby:Clone()

GameMap.Name = "Map"
GameMap.Parent = game.Workspace

wait(3)

ObbyModule.TeleportToObby(Contestants,GameMap)

I can’t because the game will break since math.random() doesn’t return an object it returns a number between 1 and how many Obbies I have, that’s why I’m using Random.new():NextInteger() because I want a map not a number, as for the rest of the script it still won’t work.

Here me out:
We use math.random() in a Table[]
That means that it take wahet index it is!

See here:

local Obbies = {
    Map1 = workspace.Map1,
    Map2 = workspace.Map2
}

local SelectedObby = Obbies[math.random(1,#Obbies)]

print(SelectedObby.Name) --prints: "Map1" or "Map 2"

If you are trying to choose a random map, put both maps in a folder and put it into replicatedstorage. Then, get the children of the folder and choose a random map. Clone the random map.

That’s not the problem thought, everything works fine but for some reason the map’s parent gets set to nil after a few milliseconds of parenting it to Workspace like I said in the post.

I have already done that, read my post again to know what I did.

It’s working perfectly for me, so it’s something on your part causing the error.

The code won’t help me because that’s not the problem at all.

For the second problem, were you sending a table for the player parameter?

If so, then you might want to do:

for i, v in next, players do

instead of:

for i, v in next, players:GetChildren() do

because if the players table is a nil value, the error would be:

attempt to index nil with `GetChildren`
1 Like

Alright I will try that out later, any solutions for the first problem thought?

The parent was set to nil right after you called the BeginObby function, right?

Try looking at the function to see if it changes the parent.

1 Like

Can you provide the BeginObby function?

Alright bois it turns out that this isn’t a Roblox bug, I have fixed the issue, how you might ask?

Well you see, try to guess the reason…

The reason was that THE OBBY WASN’T ANCHORED so because of that the obby would fall and eventually the world would just delete the map and it’s parent gets set to nil because of this, so next time you wonder why your map magically disappears then it’s probably because of this…

How I and no one thought that this was the reason before is beyond me, such a simple solution but no one could say it… 681766487492657153

Thanks to @lordmochibi for solving the second problem too.

1 Like