Script cloning an object too many times

the line that clones the model (31) does so 5 times and only that line and everything after, how do i fix this?

local Services = {
	["StarterGUI"] = game:GetService("StarterGui"),
	["UserInputService"] = game:GetService("UserInputService"),
	["ContextActionService"] =  game:GetService("ContextActionService"),
	["Debris"] = game:GetService("Debris"),
	["TweenService"] = game:GetService("TweenService"),
	["ReplicatedStorage"] = game:GetService("ReplicatedStorage"),
	["ServerStorage"] = game:GetService("ServerStorage"),
	["CollectionService"] = game:GetService("CollectionService")
}

local remote = Services.ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("StateMachineRemotes"):WaitForChild("MovesetSwitchEvent")

remote.OnServerEvent:Connect(function(player: Player, ...: any) 
	local char = player.Character
	local hum = char:FindFirstChildOfClass("Humanoid")
	
	local PlayerSpecial = hum:GetAttribute("Special")
	local HasModel = hum:GetAttribute("SpecialHasModel")
	
	if hum:GetAttribute("CurrentMoveset") == "Weapon" then
		hum:SetAttribute("CurrentMoveset", "Special")
		for i,SpecialFolder in pairs(Services.ServerStorage:WaitForChild("Movesets"):WaitForChild("SpecialMovesets"):GetChildren()) do
			if SpecialFolder:IsA("Folder") and SpecialFolder.Name == PlayerSpecial then
				for _,Special in pairs(SpecialFolder:GetChildren()) do
					if Special:IsA("ModuleScript") then
						local SpecialSkill = Special:Clone()
						SpecialSkill.Parent = player.Backpack.SkillsFolder.SpecialSkillsFolder
						
						if HasModel == true then
							local Model = SpecialFolder:WaitForChild("Model").Value:Clone()
							Model:SetAttribute("Player", player.Name)
							Model.Parent = workspace.FX.Models
							print(Model.Parent:GetChildren())

							for i, v in pairs(char:GetDescendants()) do
								if v:IsA("Part") and v.Name == Model.PlayerJoint:GetAttribute("AttachTo") then
									Model.PlayerJoint.Part0 = v
								end
							end

							for i, v in pairs(Model:GetDescendants()) do
								if v.Name ~= "HumanoidRootPart" then
									if v:IsA("Part") or v:IsA("Decal") or v:IsA("MeshPart") then
										Services.TweenService:Create(v, TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {Transparency = 0}):Play()
									end
								end
							end

							Services.TweenService:Create(Model.PlayerJoint, TweenInfo.new(.5, Enum.EasingStyle.Quad, Enum.EasingDirection.Out), {C0 = Model.PlayerJoint:GetAttribute("C0Aim")}):Play()
						end
					end
				end
			end
		end
		
	else
		hum:SetAttribute("CurrentMoveset", "Weapon")
		for _,SpecialMovesFolder in pairs(Services.ServerStorage:WaitForChild("Movesets"):WaitForChild("SpecialMovesets"):GetChildren()) do
			if SpecialMovesFolder:IsA("Folder") and SpecialMovesFolder.Name == PlayerSpecial then
				for _, ServerSpecialSkill in pairs(SpecialMovesFolder:GetChildren()) do
					for _,SpecialSkill in pairs(player.Backpack.SkillsFolder.SpecialSkillsFolder:GetChildren()) do
						if SpecialSkill.Name == ServerSpecialSkill.Name then
							SpecialSkill:Destroy()
							
							
							if HasModel == true then
								local specialModelName = SpecialMovesFolder.Model.Value.Name
								for _,SpecialModel in pairs(workspace.FX.Models:GetChildren()) do
									if SpecialModel.Name == specialModelName and SpecialModel:GetAttribute("Player") == player.Name then
										SpecialModel:Destroy()
									end
								end
							end
						end
					end
				end
			end
		end
		
	end
	player.PlayerGui.PlayerHUDGUI.SkillsFrame.InfoFrame.InfoText.Text = hum:GetAttribute("CurrentMoveset")
end)

I want to ask why do you store services inside table? it’s kindof pointless, isn’t it better to have them as variables?

1 Like

Your running 2 sets of code, with the same thing, so where you check a special folder,then get the children inside the special, meaning you could be having multiple specials running at the same time being handled improperly or the code you have is being ran multiple times. to check if it is being ran multiple times, you can add a print to the beginning of the event, and to see if it is being ran multiple times, I would recommend printing the variable i under the check to see if its a module script.

variables have a limit of 200 out of a function, and 100 per indent
As far as I am aware, tables have a near infinite amount.

1 Like

then it’s most likely your loops inside the script. or multiple types of the same file exist inside the special folder variable causing it.

for clarification the specials folder contains a bunch of modules that are then copied to the players backpack, pretty much everywhere that theres a check, i have added a print and it only does once, except for if HasModel == true and below

Then this is the section that is needing to be looked at

prints 6 times (even tho there are only 5 modules)

fixed it by making the model thing run before it checks for modules, separating it from the loop (i coulda done it after but it dont matter)

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