Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script wont delete attachments in certain parts?

Im having an issue with this script. It gets the children of the player's character, and then copies them, then it's supposed to get the children of the copies, and delete the Attachments in them but it wont. The attachments stay there and the character gets stuck. Here's my code:

script.Parent.Soru.OnServerEvent:Connect(function(Player, mousehit)
	if SoruCD == 0 and Player.NonStats.Stunned.Value == false and Player.Stats.SoruEXP.Value >= 5000
	and Player.Stats.Stamina.Value >= 250 then
		local Range = 150
		local Distance = (mousehit.p - Player.Character.UpperTorso.Position).Magnitude
		if Distance <= Range then
		SoruCD = 1
		Player.Stats.Stamina.Value = Player.Stats.Stamina.Value - 250
		Player.Character.UpperTorso.soru:Play()
		local direction = (mousehit.p - Player.Character.HumanoidRootPart.Position) * Vector3.new(1, 1, 1);
		direction = CFrame.new(Player.Character.HumanoidRootPart.Position, Player.Character.HumanoidRootPart.Position + direction)
		Player.Character.HumanoidRootPart.CFrame = direction
			local Parts = Player.Character:GetChildren()
			for i,v in pairs(Parts) do
			if v.ClassName == "MeshPart"
			then local V = v:Clone()
			V.Parent = workspace
			V.BrickColor = BrickColor.new("Really black")
			V.Transparency = 0.5
			V.Anchored = true
			V.CanCollide = false
			local Attachments = V:GetChildren()
			if Attachments.ClassName == "Attachment" then
			Attachments:Destroy()
			game:GetService("Debris"):AddItem(V, 2)	
			Player.Character.UpperTorso.CFrame = CFrame.new(mousehit.p)*CFrame.new(0,5,0)
			wait(2)
			SoruCD = 0
					end
				end
			end
		end
	end
end)

any idea what could be causing this? Thanks for reading

1 comment

3

indent

1 answer

local Attachments = V:GetChildren()
if Attachments.ClassName == "Attachment" then

Attachments is a table of children, and you're forgetting to loop over it.

5 comments

0

Show the fixed code for it.

2

No need in this case, the OP knows how to loop over a table of children, it's exactly what they are doing for the Parts table. It's one of those facepalm mistakes.

0

I actually tried a loop like that and it didn't work lol

0

It does work, you just did it wrong or through your very messy code you couldn't see it.

0

There is probably just another bug. For example, if the if Attachments.ClassName == "Attachment" then clause was actually being entered, the wait(2) inside would make this whole function take at least 2 seconds per MeshPart, or even worse if it ended up inside the attachments deletion loop! Probably the wait is supposed to be outside the loop?

This discussion is preserved as it was. New answers, comments, and votes are closed.