Not killing player when touched

I have a script that SHOULD kill the player when they touch the part.

The parts spawn through a different script, the parts spawn into the specified folder.
The scripts purpose is for it to detect if a player touches a part inside of the folder and then kill said player.

Here is my script. I can’t figure out whats wrong?

wait(1) --to let the folder be created (its a instance.new in other script)
local folder = workspace.MineFolder

for _,v in pairs(folder:GetChildren()) do
	if v:IsA('BasePart') then
		v.Touched:Connect(function(touched)
			
			local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
			if possibleHumanoid then
				possibleHumanoid.Health = 0
			end
		end)
	end
end

Any help would be appreciated

EDIT: THE SCRIPT IS A LOCAL SCRIPT INSIDE STARTERCHARACTERSCRIPTS

Is the script that spawns in your mines a local script?
Did you also check with print() checks in the script because that could help find if is being called or not.

HI Mate, is there anything in the output ?

The script that spawns the mines is a script in ServerScriptService.
This is the script by the way if you need it

local MineFolder = Instance.new("Folder")
MineFolder.Name = "MineFolder"
MineFolder.Parent = game.Workspace

local part = game.Workspace.MineFieldPart

local function spawnMine()
	local placeholder = Instance.new("Part")
	placeholder.Parent = workspace.MineFolder
	placeholder.Size = Vector3.new(2,0.1,2)
	placeholder.Name = "Mine"
	placeholder.BrickColor = BrickColor.random()
	placeholder.Material = Enum.Material.Neon
	placeholder.Shape = Enum.PartType.Ball
	placeholder.Transparency = 0
	placeholder.Anchored = true
	local random1 = math.random(-part.Size.X,part.Size.X)/2
	local random2 = math.random(-part.Size.Z,part.Size.Z)/2
	placeholder.Position = part.Position+Vector3.new(random1,part.Size.Y/2,random2)
end

while wait(1) do
	spawnMine()
end

Nothing in the output. Not sure why, but it only works on about 10% of the parts.

ok cool so the mines are looped to spawn continueously but the first script only runs once do the mine work at first then as it goes on stop working?

1 Like

So the script should look like this instead?

wait(1)
local folder = workspace.MineFolder

while true do
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA('BasePart') then
			v.Touched:Connect(function(touched)

				local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
				if possibleHumanoid then
					possibleHumanoid.Health = 0
				end
			end)
		end
	end
end

edit: nope, this crashed my studio :sob:

i think try that that way they are in sync with each other see if it help :+1:

Gotta add a wait in there somewhere otherwise it will keep crashing

move the wait to inside the loop

Got it!
Thank you so much!
You aswell @FisheeGames !

Here is the final result

wait(1)
local folder = workspace.MineFolder

while true do
	wait(1)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA('BasePart') then
			v.Touched:Connect(function(touched)

				local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
				if possibleHumanoid then
					possibleHumanoid.Health = 0
				end
			end)
		end
	end
end

(map is just for testing, final will not look anything like this)
Video :

Awesome bro glad to hear it is working :+1: