Object attached to character

Hi, as per title I want to attach an object to every player character server side. I followed this tutorial “https://www.youtube.com/watch?v=ftArvcVG26U&t” but for some reasons it doesn’t work…

That’s the script:

local players = game:GetService("Players")
local ss = game:GetService("ServerStorage")

local dummyR15 = ss.RigR15

function weld(partA, partB, offsetCFrame)
	partA.CFrame = partB.CFrame * offsetCFrame
	
	local weldConstraint = Instance.new('WeldConstraint')
	weldConstraint.Parent = partA
	weldConstraint.Part0 = partA
	weldConstraint.Part1 = partB
	
end

function onCharacterAdded(character)
	local newWeld = dummyR15.Flashlight:Clone()
	local weldPart = character.UpperTorso
	weld(newWeld, weldPart, newWeld.WeldPart.Value.CFrame:Inverse() * newWeld.CFrame)
	newWeld.Parent = character
end

players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(onCharacterAdded)
end)

for i, player in pairs(players:GetPlayers()) do
	player.CharacterAdded:Connect(onCharacterAdded)
	if (player.Character) then
		onCharacterAdded(player.Character)
	end
end

I think the problem is here

function weld(partA, partB, offsetCFrame)
	partA.CFrame = partB.CFrame * offsetCFrame
	
	local weldConstraint = Instance.new('WeldConstraint')
	weldConstraint.Parent = partA
	weldConstraint.Part0 = partA
	weldConstraint.Part1 = partB
	
end

as the weldconstraints gets created and it has the right parts in it but it just doesn’t link the 2 parts togheter (A flashlight and the UpperTorso in this specific case).
Any ideas?

2 Likes

does it appear at all on the character?

Yes it does but it falls to the ground if not anchored and doesn’t move if anchored

can you go in-game and show me the properties of the weld, and check if they are actually set up right?

whats the position of the flashlight?

It should be above his right shoulder but right now is way above because I did play here (green box in the picture)

You can also see there’s not a link to the upper torso in the picture above otherwise it would look like the picture below

try :WaitForChild() for the upper torso.

Tried. It doesn’t work anyway.

local weldPart = character:WaitForChild(newWeld.WeldPart.Value.Name)

That’s what I used as it’s the same as

local weldPart = character:WaitForChild("UpperTorso")

because the name of the value in WeldPart (an ObjectValue) is the UpperTorso of the cloned rig

Unless… Thinking about it… It might try to link to the one in the rig instead of the player one

Just kidding. It doesn’t work anyway

Perhaps see if the object is unanchored? Try testing it with the object anchored or unanchored.

Make sure to enable the WeldConstraint:

local weldConstraint = Instance.new('WeldConstraint')
weldConstraint.Part0 = partA
weldConstraint.Part1 = partB
weldConstraint.Enabled = true
weldConstraint.Parent = partA
1 Like

Tried that many times. If it’s anchored it just stays where it spawns if it isn’t it falls to the ground.

It is enabled. You can also see it from the screenshots above

I did something similar recently but i made it an accesory tied to player’s front torso, is there any reason as to why you can’t use an accesory (also i think a Weld is better than a WeldContraint for this but not certain)

No reasons. The only thing I care about is that it attaches to the player and I can modify the properties of a spotlight in the mesh. So if you are able to tell me how to do it I can try it and see if it works.

Guess what? A simple

wait(5) 

at the start of the script made it work. What I’m thinking at this point is that even if there is the

character:WaitForChild("UpperTorso") 

the script gets executed before the UpperTorso gets fully loaded which leads to it not get linked to the flashlight. Thanks to everyone who tried to help me!

1 Like

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