How to create parts for every letter?

Hi, I am making a game where you type the answer, then blocks appear below you rising you up and escape the lava. However the player can type the answer, I have no idea how to clone the part however long the answer was in letters amount of times, and find if the answer was correct or not. Can someone help me? Thank you so much. Here is my current code:

local questions = {
	"Name an animal",
	"Name an US President"
}
local answers = {
	"dog",
	"cat",
	"mockingjay",
	"goldfish",
	"George Washington",
	"Barack Obama",
	"Abraham Lincoln"
}


game.ReplicatedStorage.SendAnswer.OnServerEvent:Connect(function(player,answer)
		if answer == table.find(answers,answer) then
			local length = #answer
			for i = 1,length do
				local step = Instance.new("Part"):Clone()
				step.Name = "Step"
				step.Size = Vector3.new(5,1,5)
				step.CFrame = player.Character:FindFirstChild("HumanoidRootPart").CFrame
				step.Parent = ChosenMap
			end
		end
end)

	repeat
		game.ReplicatedStorage.SafeAnswer:FireAllClients()
		Status.Value = questions[math.random(1,#questions)]

		wait(10)

		workspace.Lava.Size += Vector3.new(0,10,0)
1 Like

To create a part for every letter, you need to check the length of the word. After receiving that number, use a for loop to loop that many amount of time based on the length.

Psuedocode:

for startingIndex, lengthOfWord do
     clone part
end

I tried that but somehow my code doesn’t work, its not instance.new()-ing the part so whenever i try and search for it in explorer, theres nothing there.

By looking at your code, when you do Instance.new(“Part”), the part is already made in which you don’t need a :Clone() after. The part should also appear under “ChosenMap” since you parent it in there.

Yes, except it is not there. I removed the :Clone() like you said I am answering the question and when I get it correct, there are no parts being added to the game or chosen map.

Try setting the name of the clone part to something unique like “testingPartName” and search it inside your explorer with the search bar to see where that part ends up. Either that or print out the name of the parent’s name to see if it is actually in the “ChosenMap”.

The function does not even start. Also did I forget to mention that this is in a while loop? Sorry about that :sweat_smile:. Here is the full code:

local RS = game.ReplicatedStorage
local SS = game.ServerStorage
local Maps = SS:WaitForChild("Maps")
local Status = RS:WaitForChild("Status")
local intermissionLength = 15
local questions = {
	"Name an animal",
	"Name an US President"
}
local answers = {
	"dog",
	"cat",
	"mockingjay",
	"goldfish",
	"george washington",
	"barack obama",
	"abraham lincoln"
}

while true do
	--Status.Value = "Waiting for enough players! (1/2)"
	--repeat wait() until game.Players.NumPlayers >=2
	Status.Value = "Intermission:"

	task.wait(3)

	Status.Value = "15"
	for i=intermissionLength,0,-1 do
		wait(1)
		local new = tonumber(Status.Value)-1
		Status.Value = tostring(new)
	end
	--END OF INTERMISSION

	local plrs = {}

	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) --add plr

		end
	end

	local AvailableMaps = Maps:GetChildren()
	local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]

	Status.Value = ChosenMap.Name.. " chosen"

	wait(2)

	Status.Value = "Game starting!"

	wait(2)

	local ClonedMap = ChosenMap:Clone()
	--ClonedMap:SetPrimaryPartCFrame(CFrame.new(490.263, 78.957, -4.343))
	if ClonedMap.Name == "Radioactive" then
		ClonedMap:PivotTo(CFrame.new(490.263, 79.457, -4.343) * CFrame.Angles(0, 0, math.pi)) -- may need to use X axis instead)
	elseif ClonedMap.Name == "Construction" then
		ClonedMap:PivotTo(CFrame.new(490.263, 35.957, -4.343))
	end
	ClonedMap.Parent = workspace

	local SpawnPoints = ChosenMap:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		warn("No spawnpoints found")
	end

	--END OF CHOOSING MAP

	local AvailableSpawns = SpawnPoints:GetChildren()

	for i,player in pairs(plrs) do
		if player then
			local char = player.Character

			if char then
				char:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawns[1].CFrame + Vector3.new(0,2,0)
				table.remove(AvailableSpawns,1)

				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = char
			else
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end

	--END OF TELEPORTING PLAYERS

	Status.Value = "Get ready to play!"

	for i,player in pairs(plrs) do
		if player then
			local character = player.Character

			if not character then
				print("Player left the game")
				table.remove(plrs,i)
			elseif character:FindFirstChild("GameTag") then
				print(player.Name.. " is still in the game!")
			else
				table.remove(plrs,i)
			end

		else table.remove(plrs,i)
			print(player.Name.. " has been removed")

		end
	end

	--END OF PLAYER DIED/REMOVED/LEFT
	game.ReplicatedStorage.SendAnswer.OnServerEvent:Connect(function(player,answer)
		if answer == table.find(answers,answer) then
			local length = #answer
			string.lower(answer)
			for i = 1,length do
				local step = Instance.new("Part")
				step.Parent = ClonedMap
				step.Color = Color3.new(0.901961, 1, 0.576471)
				step.Name = "testingStep"
				step.Size = Vector3.new(5,1,5)
				step.CFrame = player.Character:FindFirstChild("HumanoidRootPart").CFrame - Vector3.new(0,1,0)
				print(step.Parent)
			end
		end
	end)

	repeat
		game.ReplicatedStorage.SafeAnswer:FireAllClients()
		Status.Value = questions[math.random(1,#questions)]

		wait(10)

		ClonedMap.Lava.Size += Vector3.new(0,10,0)

	until #plrs == 0
	
	if #plrs == 1 then
		if plrs[1].Character:FindFirstChild("GameTag") then
			Status.Value = "The winner is: " ..plrs[1].Name
			plrs[1].leaderstats.Wins.Value += 1
		end
	elseif #plrs == 0 then
		Status.Value = "Nobody won!"
	end

	print("End of game")
	task.wait(2)

	for i,player in pairs(game.Players:GetPlayers()) do
		local charac = player.Character

		if not charac then
			print("Player does not have character!")
		elseif charac then
			if charac:FindFirstChild("GameTag") then
				charac.GameTag:Destroy()
				player:LoadCharacter()
			end
		end
	end

	ClonedMap:Destroy()

	Status.Value = "Game ended!"

	task.wait(15)

	--END OF GAME ENDING
end

I changed up the function of the answer a little bit so its all lowercase if the player gets it correct but has different capitilization.

The function is a bit at the end of the script, like 2/3’s

Should I do this in a different script? However that would break all local variables I have with this script making it nearly impossible for me to do it.

3 Likes