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

an i,v in pairs not working?

I am trying to make a script that enabled all of a certain object. It works but theres an error that I don't know what it is.

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(char)
while true do
	game.ServerStorage.Map.Value = 1
	local childs = game.Workspace.Map1:GetChildren("SpawnLocation")
	for i,v in pairs(childs) do 
		v.Enabled = true
		end
	char:BreakJoints()
	wait(15)
	game.ServerStorage.Map.Value = 2
	local childs = game.Workspace.Map1:GetChildren("SpawnLocation")
	for i,v in pairs(childs) do 
		v.Enabled = false
		end
	local childs = game.Workspace.Map2:GetChildren("SpawnLocation")
	for i,v in pairs(childs) do 
		v.Enabled = true
end
	char:BreakJoints()
end
end)
end)

It says theres an issue with line 7

1 comment

0

What's the actual error text?

1 answer

✓ Accepted answer

The problem is in lines 5, 12 & 16. you can't find the SpawnLocations just by passing "SpawnLocation" to GetChildren, because it GetChildren doesn't have any arguments. You have to check if each value is a spawnlocation, and enable it if it is.

game.Players.PlayerAdded:connect(function(player)
player.CharacterAdded:connect(function(char)

local function enable(value,toggle) --a function we will use to enable and disable spawn locations
    if value:IsA("SpawnLocation") and toggle then
        v.Enabled = true
    end
    if value:IsA("SpawnLocation") and not toggle then
        v.Enabled = false
    end
end

while true do
    game.ServerStorage.Map.Value = 1
    local childs = game.Workspace.Map1:GetChildren()
    for i,v in pairs(childs) do 
        enable(v,true)
        end
    char:BreakJoints()
    wait(15)
    game.ServerStorage.Map.Value = 2
    local childs = game.Workspace.Map1:GetChildren()
    for i,v in pairs(childs) do 
        enable(v,false)
        end
    local childs = game.Workspace.Map2:GetChildren()
    for i,v in pairs(childs) do 
        enable(v,true)
end
    char:BreakJoints()
end
end)
end)


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