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

how can I Get Childrens of a Folder, when Players touch any child, they die?

So Basically, when a random map is chosen, and if players touch on it, they are supposed to die, but it's not working, Error: ServerScriptService.Main:27: attempt to call a nil value

local mapsStorage = game.ServerStorage.Maps:GetChildren()
local randomMapChoser = mapsStorage[math.random(1, #mapsStorage)]
print("The Random map is: "..randomMapChoser.Name)
local randomMapClone = randomMapChoser:Clone()
randomMapClone.Parent = workspace.currentMap

-- kill players when touch map
local mapsParts = mapsStorage:GetChildren()

mapsParts.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Character.Humanoid.Health = 0
	end
end)

1 answer

You are referring to the table. You need to refer to the members of the table.

Here is the fixed script:

local mapsStorage = game.ServerStorage.Maps:GetChildren()
local randomMapChoser = mapsStorage[math.random(1, #mapsStorage)]
print("The Random map is: "..randomMapChoser.Name)
local randomMapClone = randomMapChoser:Clone()
randomMapClone.Parent = workspace.currentMap
	 
-- kill players when touch map

for _, map in pairs(mapsStorage) do
	 
map.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		hit.Parent.Character.Humanoid.Health = 0
		end
	end
end)

Here, instead of referring to the table, we use a for loop to iterate through the members of the table and add the event in each of them.

Hope this helps!

2 comments

0

ServerScriptService.Main:27: attempt to call a nil value, on this line : local mapsParts = mapsStorage:GetChildren()

0

Edited my script. Should work now. The issue was you were getting a table from a table.

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