What is wrong with my script?I want make a part that if you touched that part you will die slowly

bandicam 2020-09-06 19-01-42-397|623xbandicam 2020-09-06 19-04-24-369|297x129 111

you need to put a wait() condition in-between each index else the script will run all the way through instantly and kills the player

1 Like

bandicam 2020-09-06 19-04-24-369 it still like this:(

1 Like
local Infected = {}


script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player ~= nil then
		if not table.find(Infected, Player.Name) then
			table.insert(Infected, Player.Name)
			local Character = hit.Parent
			local Humanoid = Character:FindFirstChild("Humanoid")
			if Humanoid ~= nil then
				spawn(function()
					pcall(function()
						while true do 
							wait(0.001)
							if Humanoid.Health > 0 then
								Humanoid:TakeDamage(0.15)
							else
								break
							end
						end
					end)
				end)
			end
		end
	end
end)
2 Likes

oh I see the problem, you’re using player.Parent, which is the Player service, to get the character you need to use player.Character

edit: nevermind I forgot, the .Touched function returns the touched part, the problem you were running into was that the part being touched didn’t have a humanoid, which could be fixed with

if player.Parent:FindFirstChild("Humanoid") then
    for i = 0, 100, 5 do
         player.Parent.Humanoid.Health = i
    end
end
2 Likes
local Infected = {}


script.Parent.Touched:Connect(function(hit)
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
	if Player ~= nil then
		if not table.find(Infected, Player.Name) then
			table.insert(Infected, Player.Name)
			local Character = hit.Parent
			local Humanoid = Character:FindFirstChild("Humanoid")
			if Humanoid ~= nil then
				spawn(function()
					pcall(function()
						Connection = Humanoid.Died:Connect(function()
							Connection:Disconnect()
							for i,v in pairs(Infected) do
								if v == Player.Name then
									table.remove(Infected, i)
								end
							end
						end)
						while true do 
							wait(0.001)
							if Humanoid.Health > 0 then
								Humanoid:TakeDamage(0.15)
							else
								break
							end
						end
					end)
				end)
			end
		end
	end
end)
2 Likes

can you explain(detail) that to me i don"t understand LOL

can you explain(detail) that to me cuz i don"t understand;-; how can you script so good

It should be…

for i = 100, 0, -5 do

You’re removing the health, not adding it. Thefore you should use minus 5 instead of + 5.

2 Likes

@kylerzong there’s no need to overcomplicate it. OP, you could do something like this if you want the player to “die slowly” once they touched a part.

   script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid")then
		local humanoid = hit.Parent:FindFirstChild("Humanoid")
		for i = humanoid.Health, 0, -5 do
			humanoid.Health = i
			print(humanoid.Health)
			wait(1)
		end
	end
end)

We’re still using a touched event, passing through an argument called “hit”, detecting if hit’s parent is humanoid. If it’s we subtracting the player’s health by -5 each second.

If you look into the output, you would notice…

   100
    95
    90
    85
    and so on.
1 Like

wow:o it is easy do understand more than that one.-. why they make so OP;-; hard for new scripter to understand;-;

You could do that but I dont like my script running 200 times for 1 player

image

1 Like