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

When Players touch any part on the Parent's Child, they should die?

Basically, I want to make it, that the parts that are in the Maps, everytime they touch part, they are supposed to die

Error: Touched is not a valid member of Folder This line: map.Touched:Connect(function(hit)

--load map
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

for _, map in pairs(mapsParts) do

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

1 comment

0

You could include a value inside each part that says if it should kill the player. Then if it was touched check that value. But that's a terrible way so don't do that lol.

1 answer

Hello.


Problems:

  • You did not check if the part was an actual part.

  • You used hit.Parent.Character.


Solutions:

  • Add a simple if statement checking if the child is a BasePart.

  • Instead of using hit.Parent.Character, use hit.Parent:BreakJoints(). You probably thought hit.Parent was the player.


Fixed Code:

--load map
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

for _, map in pairs(mapsParts) do
	if map:IsA("BasePart")
		map.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent:BreakJoints()
			end
		end)	
	end
end

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