Studio freezing from a for loop

You can write your topic however you want, but you need to answer these questions:

  1. I am trying to recreate the fire death effect from
    psychic playground - Roblox
    (Cannot upload video, I’ll try again later)

  2. When I attempt to try and test the lava, studio freezes like so:
    (Cannot upload video, I’ll try again later)

3: This is my code.
Script:

function onTouched(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil and hit:FindFirstChild("Burn") == nil then
		hit.Parent.Humanoid.Health = 0
		local v = hit.Parent:GetDescendants()
		for i = 1, #v do
			if v[i]:IsA("BasePart") then
				local bloodthing = v[i]:Clone()
				bloodthing.Parent = v[i].Parent
				bloodthing.CFrame = v[i].CFrame
				local w = Instance.new("WeldConstraint")
				w.Parent = v[i]
				w.Part0 = v[i]
				w.Part1 = bloodthing
				local bsc = script.Parent.Burn:Clone()
				bsc.Parent = bloodthing
				bsc.Disabled = false
				v[i].Material = Enum.Material.Sand
				v[i].Color = Color3.fromRGB(99, 95, 98)
			end
		end
	end
end

connection = script.Parent.Touched:connect(onTouched)

Burn:

local TweenService = game:GetService("TweenService")
local info = TweenInfo.new(
	1,
	Enum.EasingStyle.Linear,
	Enum.EasingDirection.Out,
	0,
	false,
	1
)
local Goals = {
	Transparency = 1,
}
local function fadify(obj)
	if obj:IsA("BasePart") then
		local PartTween = TweenService:Create(obj, info, Goals)
		PartTween:Play()
		obj.Size += Vector3.new(0.01, 0.01, 0.01)
		obj.Material = Enum.Material.Neon
		obj.BrickColor = BrickColor.new("Neon orange")
	elseif obj:IsA("Decal") then
		local PartTween = TweenService:Create(obj, info, Goals)
		PartTween:Play()
	end
end
local v = script.Parent:GetChildren()
for i = 1, #v do
	if v[i]:IsA("Accessory") or v[i]:IsA("Shirt") or v[i]:IsA("ShirtGraphic") or v[i]:IsA("Pants") then
		v[i]:Destroy() 
	end
end

fadify(script.Parent)
script:Destroy()

What is wrong with this code?

By freezing do you just mean a lag spike or is studio completely crashing?

It goes into “Not Responding” state and I have to force quit using task manager.

Add a wait() on one of the loops to idnetify the problem

Oh sorry, I forgot to provide context. Burn and Script are both in a baseplate kind part, and when I jump in, it froze

I’ll try
charssssssssssssssssssssss

maybe put a bool value that chekcs if he got infected and then it makes him transform if its already false …

That could be because your onTouched function has no debounce, so it ends up inserting parts and scripts into all of your character’s descendants millions of times. You have to label the character in some kind of way that tells the script that it already touched it. You could use a table, CollectionService, attributes, or BoolValues, whatever you see fit.

Thats wh he should do so that it checks if he already got transfromed and if he didnt tit transforms himself …

I’ll attempt to put wait 1 second debounce and see if it works.

Wdym atttributes ? You can only set them manually ? How to make one via script ?

You can set attributes via Instance:SetAttribute(attributeName, attributeValue), and retrieve them via Instance:GetAttribute(attributeName)

1 Like

Ohhhh and they can be anythiong like an objct value, an it value , a string value ?

The wait 1 second isnt working, ill try to use a boolean to see if it works.

They can be whatever values attributes support, they’re not restricted to “xValue” objects.

1 Like

Have you considered my reply? If you just use a boolean, then it’s not gonna function forever after someone has touched it.

I’ll put the boolean as an attribute and see

Seems to work good! Thank you so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.