Cloning not working properly

Hi, for the past few days I’ve been trying to squash this pesky bug of mine where a sword will not clone into a player’s character. This only happens in the live game, and it works fine in Roblox Studio. I’ve tried a number of different things and I’ve commented out enough code to get to the point where nothing should be wrong from either a logical or syntactical standpoint.

Here’s the important section of code that’s left (Server Script):

--Assume item is a previously made variable containing the instance of the sword in server storage
local character = player.Character or player.CharacterAdded:Wait()
local charSword = item:Clone()

charSword.Name = "Sword"
charSword.Parent = character

--events.SwordReplicateEvent:FireClient(player)

The sword is a model NOT a tool, its CFrame does get set to the character’s hand, and it has a motor6D connecting it to the hand later in the script (except that section doesn’t work because Sword is not a valid member of Character).

4 Likes

If it’s a tool, are you parenting to the player’s Backpack? That might be why…

It’s a model, not a tool. Sorry for not clarifying.

Think I misread the character as player, lol.

You might want to weld it to one of your player’s limbs and/or HumanoidRootPart via Motor6Ds. If you don’t, it’ll just fall off.

Just because you parent it to the character doesn’t mean it’ll be on the character. You’d have to weld and reposition the model to the character when you clone it. Otherwise, it will stay at the position you put it in in studio.

@sansprosii @VegetationBush

It does get attached by a motor6D later in the script

Perhaps clone the tool to the backpack, and then have the tool automatically equipped. Disregard, I just read that it isn’t a tool.

Update: Added a thing to set CFrame and print out position, it essentially prints (0, 0, 0), that shouldn’t be right since the player spawn is not anywhere near (0, 0, 0).

local character = player.Character or player.CharacterAdded:Wait()
local charSword = item:Clone()

charSword.Name, charSword.BodyAttach.CFrame = "Sword", character.RightHand.CFrame
charSword.Parent = character

print(character.Sword.Hitbox.Position)
--events.SwordReplicateEvent:FireClient(player)

The issue has been solved, in conclusion:

The bug was caused because the script would run before the player’s character was in workspace so that when it was eventually parented to workspace it would cause issues with the sword not being cloned to the character.

Best 3 lines of code for when you clone stuff into the character:

if not character:IsDescendantOf(workspace) then
	character.AncestryChanged:Wait()
end

I’m not really sure why this causes issues, but it does.

2 Likes