1

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

EEnergy8 247
2 months ago

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
2Loos 145
2 months ago

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