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
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.
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.
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.
@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.