I,v in pairs help

I want it so when the humanoid dies, the body slowly fades away, but it’s not working.

Code

script.Parent.Humanoid.Died:Connect(function()
	print("lol dead")
	for i,v in pairs(script.Parent:GetChildren()) do
		if v:IsA("BasePart") then
			v.Transparency = 0.8
			wait(.3)
			v.Transparency = 0.7
			wait(.3)
			v.Transparency = 0.6
			wait(.3)
			v.Transparency = 0.5
			wait(.3)
			v.Transparency = 0.4
			wait(.3)
			v.Transparency = 0.3
			wait(.3)
			v.Transparency = 0.2
			wait(.3)
			v.Transparency = 0.1
			wait(.3)
		end
		
	end
end)

Where is the script placed, in the character?

no, i made a tool were when you hit the model, it kills it.

the script is located in the model.

The way you are doing it is not efficient. You should instead use TweenService.

local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
	2, -- Duration of the tween
	Enum.EasingStyle.Linear, -- Easing style
	Enum.EasingDirection.Out, -- Easing direction
	0, -- how many times it repeats
	false, -- if it should reverse
 	0 -- delay if the animation is repeating
)
script.Parent.Humanoid.Died:Connect(function()
	print("lol dead")
	for i,v in pairs(script.Parent:GetChildren()) do
		if v:IsA("BasePart") then
			local tween = TweenService:Create(v, tweenInfo, {Transparency = 1})
			tween:Play()
		end
	end
end)
1 Like

I would recommend you use the TweenService to do this. Since this will allow you to apply a tween to all of the body parts and allow them to be syncronized.

Sorry @BPilot253 didn’t see what you just posted

1 Like

Well, when the character is respawning after it dies, all the model itself is respawning, including the script. Try to make the time to fully change the transparency of all the body parts shorter than the time it takes for the character to respawn. Also, it is better to use TweenService as @xZylter and @BPilot253 said.

1 Like

Tested your script, comes up with error Expected idenifier while parsing expression, got ‘)’, im sorry, im new at coding.

Try this script:

script.Parent.Humanoid.Died:Connect(function()
	
	print("lol dead")
	
	for i,v in pairs(script.Parent:GetChildren()) do
		
		if v:IsA("BasePart") then
			
			local tweenService = game:GetService("TweenService");
			
			local tweenInfo = TweenInfo.new(
				game:GetService("Players").RespawnTime - 1,
				Enum.EasingStyle.Quad,
				Enum.EasingDirection.Out
			)
			
			local goal = {
				Transparency = 1;
			}
			
			local finalTween = tweenService:Create(v, tweenInfo, goal)
			
			finalTween:Play()
		end
	end
end)
1 Like

Sorry, I added one extra comma. I fixed it and it should work now :slight_smile:

Thanks! This worked, tweening is a lot better anyways.

1 Like