I am making a script that gives you an aura but it only works sometimes. Why?

game.Players.PlayerAdded:Connect(function(plr)
	
	wait()
	
	local elementdata = plr:WaitForChild("ElementData")
	local elementtf = elementdata:WaitForChild("HasElement")
	local elementtype = elementdata:WaitForChild("ElementType")
	
	repeat wait() until elementtf.Value == true and (elementtype.Value ~= "NA" and #elementtype.Value ~= 0)
	
	plr.CharacterAdded:Connect(function(char)
		if elementtype.Value == "Ice" then
			local aura = game.ReplicatedStorage.ElementAuras.IceAura:Clone()
			aura.Anchored = false
			aura.CanCollide = false
			aura.Parent = char
			aura.Name = aura.Name.."/"..plr.Name
			local bp = Instance.new("BodyPosition")
			bp.Parent = aura
			bp.Position = char:WaitForChild("HumanoidRootPart").Position + Vector3.new(0,-2, 0)
			wait(1)
			
			while true do
				wait()
				bp.Position = char:WaitForChild("HumanoidRootPart").Position + Vector3.new(0,-2, 0)
			end
			
		elseif elementtype.Value == "Fire" then
			local aura = game.ReplicatedStorage.ElementAuras.FireAura:Clone()
			aura.Anchored = false
			aura.CanCollide = false
			aura.Parent = char
			aura.Name = aura.Name.."/"..plr.Name
			local bp = Instance.new("BodyPosition")
			bp.Parent = aura
			bp.Position = char:WaitForChild("HumanoidRootPart").Position + Vector3.new(0,-2, 0)
			wait(1)
			
			while true do
				wait()
				bp.Position = char:WaitForChild("HumanoidRootPart").Position + Vector3.new(0,-2, 0)
			end
			
			
		elseif elementtype.Value == "Earth" then
			local aura = game.ReplicatedStorage.ElementAuras.EarthAura:Clone()
			aura.Anchored = false
			aura.CanCollide = false
			aura.Parent = char
			aura.Name = aura.Name.."/"..plr.Name
			local bp = Instance.new("BodyPosition")
			bp.Parent = aura
			bp.Position = char:WaitForChild("HumanoidRootPart").Position + Vector3.new(0,-2, 0)
			wait(1)
			
			while true do
				wait()
				bp.Position = char:WaitForChild("HumanoidRootPart").Position + Vector3.new(0,-2, 0)
			end
			
		end
	end) 
end)

5 comments

0

What errors did you get?

0

None

0

This line: repeat wait() until elementtf.Value == true and (elementtype.Value ~= "NA" and #elementtype.Value ~= 0) waits for the Value to be true but it can be only the ones in the if.

0

So I figured out it has to be a problem with CharacterAdded because if I reset it works but I'm not sure how to make it work off of the first CharacterAdded

0

Try putting local char = plr.Character or plr.CharacterAdded:Wait(). Instead of CharacterAdded, and wrap the auras in function so then when the Humanoid Died Event fires, just call that function