Infinite yield possible on 'Workspace.a Sword Fight:WaitForChild("ClassicSword")'

I want to teleport my character to the ‘Sword Fight’ place, hand me a sword, and then when the timer ends, the sword destroys from my inventory.

The problem is not an error, is a warning(it’s typed with yellow/orange)

There’s the code:

local children = workspace.Ingame:GetChildren()
		for i = 1,#children do
			map:WaitForChild("ClassicSword"):Clone().Parent = children[i]]  --problem
		end

There’s the full code:

local status = game.ReplicatedStorage.Status
local maps = game.ReplicatedStorage.Maps:GetChildren()

while true do
	for i = 1,10 do
		status.Value = "Intermission: "..10-i
		wait(1)
	end
	
	local rand = math.random(1, #maps)
	
	local map = maps[rand]:Clone()
	map.Parent = workspace
	
	status.Value ="We'll be playing "..map.Name.."!"
	wait(4)
	
	local players = game.Players:GetChildren()
	for i = 1,#players do
		if players[i].Character ~= nil then
			local spawnLocation = workspace.Teleports:FindFirstChild(map.Name)
			players[i].Character:MoveTo(workspace.Teleports:FindFirstChild(map.Name).Position)
		    players[i].Character.Parent = workspace.Ingame
		end
	end
	
	local roundLenght = 0
	local CanWin = true
	local roundType = ""
	
	if map:FindFirstChild("Obby") then
		roundType = "Obby"
		map.EndPart.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				CanWin = false
				status.Value = hit.Parent.Name.." has won!"
			end
		end)
	elseif map:FindFirstChild("Sword") then
		roundType = "Sword"
		
		local children = workspace.Ingame:GetChildren()
		for i = 1,#children do
			map:WaitForChild("ClassicSword"):Clone().Parent = children[i]]
		end
		
		map:FindFirstChildWhichIsA("Tool").Destroy()
	end
	
	
	if map.Name == "the Lava Race" then
		roundLenght = 60
	end
	
	if map.Name == "a Sword Fight" then
		roundLenght = 10
	end
	
	repeat
		roundLenght = roundLenght -1
		status.Value = "Time Left: "..roundLenght
		wait(1)
	until roundLenght == 0 or CanWin == false or #workspace.Ingame:GetChildren() == 0 or (#workspace.Ingame:GetChildren() == 1 and roundType == "Sword")
	

	
	
	if #workspace.Ingame:GetChildren() == 1 and roundType == "Sword" then
		status.Value = workspace.Ingame:FindFirstChildWhichIsA("Model").Name.." has won!"
		wait()
		workspace.Ingame:FindFirstChildWhichIsA("Model"):MoveTo(workspace.SpawnLocation.Position)
	end	
	
	if roundLenght == 0 then
		status.Value = "Time's up!"
	end
	
	wait(3)
	
map:Destroy()
end
2 Likes

This means the code cannot find the item Classic Sword (It is infinitely waiting as it doesn’t exist)

Try checking ClassicSword is actually part of map.

1 Like

WaitForChild is extremely important when working on code ran by the client (in a LocalScript), so, if this code isn’t localized into a LocalScript, you shouldn’t use WaitForChild, or instead of using WaitForChild try using FindFirstChild

It is. The map is a variable. In my explorer, all the maps are inserted in ReplicatedStoarge in a folder. And “map” means a random map from there. And that “map” from the litle script, is the “sword fight” map.

AS FeaturedLua said use FindFirstChild as WaitForChild is for localized scripts mainly

I tried used “FindFirstChild”. This problem right there, is made after i changed “FindFirstChild” with “WaitForChild”.

If i use “WaitForChild” It’s give me this error: "Attempt to index nil with “Clone”.
Can u fix that?

(Btw, thank you for the info!)

The script is unable to find ClassicSword under the variable map. Bear in mind map was parented to workspace.

If all doesnt work try to do workspace.(map name goes here):FindFirstChild:(“ClassicSword”)

1 Like

Another error ://

“attempt to call a Instance value”

Are you entirely sure that “ClassicSword” exists under the map’s group/model/folder?

I just want to tell you that an warning is not bad, Unless it is linked to an error. I have warnings in my games sometimes and the game works just fine.

Have you tried this way below?

map.ClassicSword:Clone().Parent = children[i]]

You don’t need to use WaitForChild on the server in this case. You should use FindFirstChild. FindFirstChild will never error (unless incorrect syntax is used) and will either return the instance or nil if the instance doesn’t exist. You can then check to see if the instance exists in with an if statement.

To fix “attempt to call a Instance value” do

local Instance = Instance.new()
local Value = Instance

Just adapt this script to your script. Tell me if theres an error

@andreithepro1 I wouldn’t advise storing tools in the workspace, they can easily be picked up by players and may even be falling off your map as soon as the game starts which would technically explain your error. Is there a reason you’re using workspace to store the tool itself, or am I missing something?

You set the parent of the Sword into one of the folder’s elements, and it can’t find the sword. You don’t
have to put the sword in one of the folder’s elements. just do map:FindFirstChild("ClassicSword"):Clone().Parent = workspace.InGame

workspace.InGame:FindFirstChild("ClassicSword")
also I wouldn’t recommend putting it inside a map maybe like in ServerStorage like @Zivao said.

2 Likes

That is true, I would put it in ServerStorage or ReplicatedStorage

Yeah that’s what I meant, the tools shouldn’t really be exposed to workspace.

@andreithepro1 Did you try any suggestion and did they work/what happend wrong