i have a function that im trying to test. this is the test function.
local function createGhostModel(model)
if not model:IsA("Model") then return end
print(model)
local newModel = model:Clone()
print(newModel)
for i,v in ipairs(newModel:GetDescendants()) do
if v:IsA("Motor6D") then
v:Destroy()
elseif v:IsA("BasePart") then
v.Anchored = true
end
end
end
for some odd reason, every time i try to use the function with something , an error in the output implies that the newModel variable is nil. even though the character model returns fine. I genuinely don’t understand what is wrong and this is how i got the character model.
player.CharacterAdded:Connect(function(char)
character = char
createGhostModel(char)
end)
if anyone can help me find the issue thanks in advance
the function is a LocalScript in StarterPlayerScripts
What do you mean it returns nil? The function createGhostModel isnt supposed to return anything since you didn’t make it return anything.
Assuming what you want is to clone the character though, make sure you set model.Archivable = true before doing model:Clone(), then newModel will be the cloned model.
local function createGhostModel(model)
if not model:IsA("Model") then return end
local archivableBefore = model.Archivable
model.Archivable = true
print(model)
local newModel = model:Clone()
print(newModel)
model.Archivable = archivableBefore
for i,v in ipairs(newModel:GetDescendants()) do
if v:IsA("Motor6D") then
v:Destroy()
elseif v:IsA("BasePart") then
v.Anchored = true
end
end
return newModel
end
Try doing a check to see if there is a motor inside the part. Maybe the motor in the part you’re trying to find in the for loop just isn’t there.
local function createGhost(part)
part.Archivable = true
local new = part:Clone()
new.CanCollide = false
new.CanQuery = false
new.CanTouch = false
local part_motor = new:FindFirstChildOfClass("Motor6D")
if part_motor then
part_motor:Destroy()
end
new.Parent = workspace
new.Anchored = true
end