Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by
Borrahh 265 Moderation Voter
4 years 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 answer

Log in to vote
1
Answered by
2Loos 168
4 years 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
Ad

Answer this question