I made the local script so when a character is added it clones the character and removes scripts in the character model. But it doesn’t parent it to the viewport frame, do the for pairs loop. or print errors ANY help is very appreciated!
local Players = game:GetService("Players").LocalPlayer
local danceFrame = script.Parent
local AnimationBox = danceFrame.ScrollingFrame.danceBox
Players.CharacterAdded:Connect(function(character)
if character then
if character:FindFirstChild("Humanoid") ~= nil then
if character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
local clone = character:Clone()
for _, v in pairs(clone:GetChildren()) do
if v:IsA("LocalScript") or v:IsA("Script") then
v:Destroy()
end
end
clone.Parent = AnimationBox.ViewportFrame
end
end
end
end)
No, still doesn’t work. It doesn’t pass through the for pairs loop.
local Players = game:GetService("Players").LocalPlayer
local danceFrame = script.Parent
local AnimationBox = danceFrame.ScrollingFrame.danceBox
Players.CharacterAdded:Connect(function(character)
if character then
if character:FindFirstChild("Humanoid") ~= nil then
if character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
character.Archivable = true
local clone = character:Clone()
for _, v in pairs(clone:GetChildren()) do
if v:IsA("LocalScript") or v:IsA("Script") then
v:Destroy()
print(v .. "was destroyed")
end
end
clone.Parent = AnimationBox.ViewportFrame
end
end
end
end)
The local script doesn’t even pass through the characteradded event, so nothing runs in the function. It only prints “before function”.
local Players = game:GetService("Players").LocalPlayer
local danceFrame = script.Parent
local AnimationBox = danceFrame.ScrollingFrame.danceBox
print("before function")
Players.CharacterAdded:Connect(function(character)
print("Start")
if character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
local clone = character:Clone()
print("character cloned")
for _, v in pairs(clone:GetChildren()) do
if v:IsA("LocalScript") or v:IsA("Script") then
v:Destroy()
end
end
clone.Parent = AnimationBox.ViewportFrame
end
end)
CharacterAdded may fire while objects within the character are being loaded. In other words, you cannot guarantee that all objects you expect to be in the character will be present when it fires. So, you should check each object as they become available connecting ChildAdded to a function.
It would be easier to simply wait for and remove any object which fulfils your criteria.
-- tip: make your removal predicate its own function rather than stuffing
-- it all in one "if" condition
local function shouldDestroy(object)
return object:IsA("Script") or object:IsA("LocalScript")
end
local function onCharacterChild(child)
if shouldDestroy(child) then
print("Destroying character object:", child:GetFullName())
child:Destroy()
end
end
Players.CharacterAdded:Connect(function (character)
for _, child in pairs(character:GetChildren()) do
task.spawn(onCharacterChild, child)
end
character.ChildAdded:Connect(onCharacterChild)
end)
That probably indicates the character already exists when the event connection was made. So you’ll need to check for this upon connecting the event, using a named (non-anonymous) function:
local function onCharacterAdded(character)
-- ...
end
if player.Character then task.spawn(onCharacterAdded, player.Character) end
player.CharacterAdded:Connect(onCharacterAdded)