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

Why I can't kill the player that touched the parts?

I don't get any error, so a random map should be chosen, then when they touch any part of that map, they die, but it's not working and i don't get any error

--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") then
        map.Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                hit.Parent:BreakJoints()
            end
        end)    
    end
end

1 answer

I'd imagine the maps stored in "Maps" are models, and since they're models, the Touched event isn't an event of them.

Also, you'd want to create a touched event for maps that you clone into the workspace. Creating a touched event for all of the parts in a map and then cloning that map will only reference the map from which you cloned, not the cloned map, so you'll have to create a touched event for each of the parts in the map that you cloned.

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

for i, v in pairs(randomMapClone:GetDescendants()) do
	if v:IsA("BasePart") then
		v.Touched:Connect(function(hit)
			-- do whatever
		end
	end
end

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