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

If player touches any part of the map, They Die?

Basically, after a countdown, a Random map is supposed to be chosen, which works fine. If player touches any part of the random selected map, they are supposed to die, but it's not working attempt to index nil with 'Connect'

local chooseRandom = game.ServerStorage:GetChildren()
local theChosenMap = chooseRandom[math.random(1, #chooseRandom)]
local cloneTheChosenMap = theChosenMap:Clone()
cloneTheChosenMap.Parent = workspace.Game.currentMap


--kill when player touches part
for i, v in pairs(theChosenMap:GetDescendants()) do
	v:GetChildren().Touched:Connect(function(hit)
		print("hi")
	end)
end

1 answer

✓ Accepted answer

No need to get the Children of v, just use an if, then statement to make sure that only Part(s), BasePart(s), and MeshPart(s) are considered. I slightly tweaked your script.

local chooseRandom = game.ServerStorage:GetChildren()
local theChosenMap = chooseRandom[math.random(1, #chooseRandom)]
local cloneTheChosenMap = theChosenMap:Clone()
cloneTheChosenMap.Parent = workspace.Game.currentMap

for i, v in pairs(theChosenMap:GetDescendants()) do
	if v:IsA("BasePart") or v:IsA("Part") or v:IsA("MeshPart") then --Makes sure to not consider scripts and cameras, etc., cause these will cause an error when the touched function is ran. 
		v.Touched:Connect(function(hit) --No need to find the children of v when you already have all the descendants!
			print("Touched")
		end)
	end
end

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