Death Effect Script Isn't Working As Intended

So currently I’m trying to make a death affect for my laser tag gun system. So far I have everything worked out, when the player is at 0 health the script properly activates. What I’m trying to achieve is make the entire character neon, blue, and anchored. So far I’m trying to use a For I, v in pairs loop, but what happens is that it just makes the head Blue and stays on the head.

Here is my death code:

local function kill(char,plr)
	while wait(.01) do
		for _, child in ipairs(char:GetChildren()) do
			if child:IsA("Part") then
				print(child)
				child.BrickColor = plr.TeamColor
			else

			end
		end

	end
end

1 Like

the parts of the body is baseparts so do if child isa part or basepart

local function kill(char,plr)
	while wait(.01) do
		for _, child in ipairs(char:GetChildren()) do
			if child:IsA("BasePart") or child:IsA("Part") then
				print(child)
				child.BrickColor = plr.TeamColor
                                child.Anchored = true
                                child.Material = "Neon"
			else

			end
		end

	end
end

the problem is that not every object inside the character is a part. Add or IsA("MeshPart") after your condition and then it should work

Alright it works now, thanks. But now it loops forever. How can I break the loop?

1 Like
local function kill(char,plr)
		for _, child in ipairs(char:GetChildren()) do
			if child:IsA("BasePart") or child:IsA("Part") then
				print(child)
				child.BrickColor = plr.TeamColor
                                child.Anchored = true
                                child.Material = "Neon"
			else

			end
		end

	end
end

to make the function work here:

local Character = character -- get character
local plr = game.Players:GetPlayerFromCharacter(Character)
local Humanoid = character.Humanoid

Humanoid:GetPropertyChangedSignal("Health"):Connect(function()
kill(Character,plr)
end)

I dont really know functions

How did you break the function? And also the second script is un-needed I pass all the needed info through the function.

1 Like

You added a while wait loop which made it run forever so all you had to do is remove while wait. You should use PropertyGetChangedSignal() so you dont need while wait loops as it helps preformance.

This is the script. (Yes I’m fully aware its terrible, I’m going to clean it up soon)

game.ReplicatedStorage.BulletHandler.OnServerEvent:Connect(function(plr, rayPos, rayDirection)
	
	local rayCastParams = RaycastParams.new()
	rayCastParams.FilterDescendantsInstances = {plr.Character}
	rayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
	local rayCastResult = workspace:Raycast(rayPos, rayDirection, rayCastParams)
	if rayCastResult then
		local laserClone = game.ServerStorage.Laser:Clone()
		laserColor(plr.TeamColor, laserClone)
		laserClone.BrickColor = plr.TeamColor
		laserClone.Position = rayPos
		laserClone.Parent = game.Workspace
		local distance = (rayCastResult.Position - rayPos).magnitude
		laserClone.CFrame = CFrame.lookAt(rayPos, rayCastResult.Position)
		laserClone.Size = Vector3.new(.1,.6,distance)
		laserClone.Position = laserClone.Position + laserClone.CFrame.LookVector * distance/2
		if rayCastResult.Instance.Parent:FindFirstChild("Humanoid") then
			print(rayCastResult.Instance.Name)
			local hitHum =  rayCastResult.Instance.Parent:FindFirstChild("Humanoid")
			if hitHum  then
				if rayCastResult.Instance.Name == "Head" then
					hitHum:TakeDamage(50)
					if hitHum.Health <= 0 then
						kill(hitHum.Parent,plr)
					end
				else
					hitHum:TakeDamage(25)
					if hitHum.Health <= 0 then
						kill(hitHum.Parent,plr)
					end
				end
			end
		end
		for i = 0 , 1, .1 do
			wait(.01)
			laserClone.Transparency = i
			if i == 1 then
				laserClone:Destroy()
			end
		end
	end
end)
1 Like

That was the only instance that I used while wait, but thanks for the tip I’ll keep it in mind the next time I need a while true loop.

1 Like

To use PropertyGetChangedSignal() you would first do the value or whatever your trying to detect so

BoolValue:PropertyGetChangedSignal("Value")
-- code
end)