Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[Solved] Why does my function print nil instead of the name when the second function works fine?

I have a script that randomly generates a set path etc but the obstacle gen function doesn't output the name it just outputs nil but the base gen function works fine (they're both basically the same)

Explorer: https://imgur.com/a/1qWea8K

Code (I know its messy and bad I'm very new to scripting in roblox):

local worldFolder = game.Workspace.ZaWarudo
local modelsFolder = game.ReplicatedStorage:WaitForChild("Models")

local randomModel
local randomName


for _, child in ipairs(worldFolder.Placements:GetChildren()) do
	child.Transparency = 1 --Sets the things transparency to 1 so that it wont show in play mode but only in editor
end

local function checkBases()
	for _, child in ipairs(worldFolder.Placements:GetChildren()) do
		if child.Name == "BigBasePlacement" then
			local models = modelsFolder:WaitForChild("Platforms"):WaitForChild("BigBase"):GetChildren()
			n = math.random(1, #models)
			randomModel = models[n]
			randomName = randomModel.Name
			theCFrame = child.CFrame
			print(randomModel.Name)
		else if child.Name == "MediumBasePlacement" then
				local models = modelsFolder:WaitForChild("Platforms"):WaitForChild("MediumBase"):GetChildren()
				n = math.random(1, #models) --select a random obstacle / base
				randomModel = models[n]
				randomName = randomModel.Name
				theCFrame = child.CFrame
				print(randomModel.Name)
			else if child.Name == "CheckpointPlacement" then
					local models = modelsFolder:WaitForChild("Platforms"):WaitForChild("Checkpoint"):GetChildren()
					n = math.random(1, #models)
					randomModel = models[n]
					randomName = randomModel.Name
					theCFrame = child.CFrame
					print(randomModel.Name)
				end
			end
		end

		--wait(0.5)

		print(randomName)
		local newGroup = Instance.new("Model")
		newGroup.Parent = worldFolder.Models
		--newGroup.Name = randomName
		--randomModel.Parent = newGroup
		--local veryNew = newGroup:Clone()
		--local yeet = randomModel.Name
		--veryNew.yeet.Parent = worldFolder.Models
		--veryNew.yeet.CFrame = theCFrame
	end
end

local function checkObstacles()
	for _, child in ipairs(worldFolder.Placements:GetChildren()) do
		if child.Name == "ObstacleBase1x1Placement" then
			local models = modelsFolder:WaitForChild("Obstacles"):WaitForChild("Obstacle1x1"):GetChildren()
			n = math.random(1, #models) --select a random obstacle / base
			randomModel = models[n] 
			randomName = randomModel.Name
			theCFrame = child.CFrame
			print(randomModel.Name)
		else if child.Name == "ObstacleBase2x1Placement" then
				local models = modelsFolder:WaitForChild("Obstacles"):WaitForChild("Obstacle2x1"):GetChildren()
				n = math.random(1, #models)
				randomModel = models[n]
				randomName = randomModel.Name
				theCFrame = child.CFrame
				print(randomModel.Name)
			else if child.Name == "ObstacleBase3x2Placement" then
					local models = modelsFolder:WaitForChild("Obstacles"):WaitForChild("Obstacle3x2"):GetChildren()
					n = math.random(1, #models)
					randomModel = models[n]
					randomName = randomModel.Name
					theCFrame = child.CFrame
					print(randomModel.Name)
				end
			end
		end
		
		--wait(0.5)
		
		print(randomName)
		local newGroup = Instance.new("Model")
		newGroup.Parent = worldFolder.Models
		--newGroup.Name = randomName
		--randomModel.Parent = newGroup
		--local veryNew = newGroup:Clone()
		--local yeet = randomModel.Name
		--veryNew.yeet.Parent = worldFolder.Models
		--veryNew.yeet.CFrame = theCFrame
	end
end


while true do
	wait(1)
	checkObstacles()
	wait(1)
	checkBases()
	wait(120)
	worldFolder.Models:ClearAllChildren()
	wait(5)
end

3 comments

0

The checkObstacles() works fine but the checkBases doesnt work since it doesnt make the randomName into the randomModel.Name for whatever reason

0

wait i mixed em up lol its the checkBases that works not checkObstacles

0

Ok turns out that in checkObstacles() i cant call any things such as .Name or do :Clone() oor .Parent etc

1 answer

Somehow making the checkObstacles go above checkBases made it work for whatever reason

1 comment

0

hi

This discussion is preserved as it was. New answers, comments, and votes are closed.