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'

01local chooseRandom = game.ServerStorage:GetChildren()
02local theChosenMap = chooseRandom[math.random(1, #chooseRandom)]
03local cloneTheChosenMap = theChosenMap:Clone()
04cloneTheChosenMap.Parent = workspace.Game.currentMap
05 
06 
07--kill when player touches part
08for i, v in pairs(theChosenMap:GetDescendants()) do
09    v:GetChildren().Touched:Connect(function(hit)
10        print("hi")
11    end)
12end

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.

01local chooseRandom = game.ServerStorage:GetChildren()
02local theChosenMap = chooseRandom[math.random(1, #chooseRandom)]
03local cloneTheChosenMap = theChosenMap:Clone()
04cloneTheChosenMap.Parent = workspace.Game.currentMap
05 
06for i, v in pairs(theChosenMap:GetDescendants()) do
07    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.
08        v.Touched:Connect(function(hit) --No need to find the children of v when you already have all the descendants!
09            print("Touched")
10        end)
11    end
12end
Ad

Answer this question