Making a flag weld to the back of a player when they pick it up, and breaking that weld when they die

Hey guys, you’ve really helped me in the past, and today it seems i’m out of luck, so instead of smashing my computer, i took a deep breath and decided to ask for help here.

I’ve been trying to make a capture the flag type game, but i’m quite new to developing so i really have no idea what i’m doing, lol.

I have a script that respawns the flag if it is destroyed, and you can see the layout of that here:

!

This is what my flag looks like:
Screen Shot 2020-06-06 at 5.09.37 PM !

This is the script I have that will weld the flag to the back of the player, but instead, it is welding it right through the player.

I would like this script to weld it right onto the back of the torso, a slight tilt to the left or right if possible. I would like the welds to break, and the flag to fall to the ground when the player dies.

How do I do this?

thanks in advance.

  1. Ur flag is welded into the center of torso for offseting the flag u should look up into C0 and C1.
  2. There exists an event humanoid.Died or something like that, here u want to check if the player was helding the flag and the destroy the weld by using :Destroy() function

Well done for starting it yourself, great to see your thought process here.

So first thing’s first, you’re going to want to add a check to the flag so that if it’s touched by another player / the touch event fires whilst the flag is welded to the player it doesn’t create more welds.

Secondly, what you need to change for the flag to be orientated behind the player and tilted to the right is the C0/C1 properties. Please check the documentation out here.

Finally, if you want it to drop when they die, just keep track of the player once they’ve touched it and then break the joint connecting the flag to the player.

local player_weld = nil --> Let's store our weld as a variable so we can check if has been instantiated

script.Parent.Touched:connect(function (t)
	if player_weld and player_weld.Parent then
		return --> this checks to see if we have a player_weld, if we do, and it's parented, we don't want to make anymore right now as it's welded to the player
	end
	if t.Parent and t.Parent:FindFirstChild 'Humanoid' then
		local char  = t.Parent
		local torso = char:FindFirstChild 'UpperTorso' or char:FindFirstChild 'Torso'
		if torso then --> We should always check if these GameObjs exist before doing anything
			if t.Parent.Humanoid.Health <= 0 then
				return --> Let's make sure the player isn't already dead
			end
			--> Let's instantiate that player weld now...
			player_weld = Instance.new('Weld', script.Parent)
			player_weld.Part0  = torso
			player_weld.Part1  = script.Parent
			player_weld.Parent = script.Parent
			player_weld.Name   = 'Flag_Weld'
			
			--[!] You need to modify the C0/C1 (the coordinate frame positions of the joint)
			--[!] It's difficult for me to know how this is going to position as I don't know the orientation of your Flag object
			--[!] But if it's front surface is the horizontal aspect of the flag this should work:
			player_weld.C0 = CFrame.new(0, 0, 0.5) * CFrame.Angles(0, 0, -math.pi/8)
			
			--> Okay, now let's track if the player dies
			t.Parent.Humanoid.Died:connect(function ()
				--> Humanoids normally break joints on death, so let's just make sure that this joint still exists...
				if player_weld then
					player_weld:Destroy()
					player_weld = nil
				end
			end)
		end
	end
end)
1 Like

thank you so much for this.

it is flawless except that it welds it to me even though its my own team’s flag. how can I make it realize that i’m on the same team?

@Johninhalesoxygen lets say once the flag is captured then the flags brick color becomes the capturers team color

if t.Parent and t.Parent:FindFirstChild("Humanoid") and game.Players[t.Parent.Name].TeamColor ~= script.Parent.BrickColor then

??? im very confused, im saying that if you touch your teams flag, it welds itself to you which is not supposed to happen, it is just supposed to stay there

thats what I did, the if statement checks if the flag is the same color as your team color and if its not then the code continues. here

local player_weld = nil --> Let's store our weld as a variable so we can check if has been instantiated

script.Parent.Touched:connect(function (t)
	if player_weld and player_weld.Parent then
		return --> this checks to see if we have a player_weld, if we do, and it's parented, we don't want to make anymore right now as it's welded to the player
	end
	if t.Parent and t.Parent:FindFirstChild("Humanoid") and game.Players[t.Parent.Name].TeamColor ~= script.Parent.BrickColor then
        script.Parent.BrickColor = game.Players[t.Parent.Name].TeamColor
		local char  = t.Parent
		local torso = char:FindFirstChild 'UpperTorso' or char:FindFirstChild 'Torso'
		if torso then --> We should always check if these GameObjs exist before doing anything
			if t.Parent.Humanoid.Health <= 0 then
				return --> Let's make sure the player isn't already dead
			end
			--> Let's instantiate that player weld now...
			player_weld = Instance.new('Weld', script.Parent)
			player_weld.Part0  = torso
			player_weld.Part1  = script.Parent
			player_weld.Parent = script.Parent
			player_weld.Name   = 'Flag_Weld'
			
			--[!] You need to modify the C0/C1 (the coordinate frame positions of the joint)
			--[!] It's difficult for me to know how this is going to position as I don't know the orientation of your Flag object
			--[!] But if it's front surface is the horizontal aspect of the flag this should work:
			player_weld.C0 = CFrame.new(0, 0, 0.5) * CFrame.Angles(0, 0, -math.pi/8)
			
			--> Okay, now let's track if the player dies
			t.Parent.Humanoid.Died:connect(function ()
				--> Humanoids normally break joints on death, so let's just make sure that this joint still exists...
				if player_weld then
					player_weld:Destroy()
					player_weld = nil
				end
			end)
		end
	end
end)
``` I just rewrote @Isocortex code and add some edits on it

oh, thank you so much.

your entire comment got cut off after one sentance, so that’s why i was confused.

@OctaLua’s code is the way to go for this, however, I would note that it is best practice ensure that the object that’s touched it is a player though.

local player_weld = nil --> Let's store our weld as a variable so we can check if has been instantiated

script.Parent.Touched:connect(function (t)
	if player_weld and player_weld.Parent then
		return --> this checks to see if we have a player_weld, if we do, and it's parented, we don't want to make anymore right now as it's welded to the player
	end
	if t.Parent and t.Parent:FindFirstChild("Humanoid") then
		--> check if the part that touched us is a part from a player's character
		local player = game.Players:GetPlayerFromCharacter(t.Parent)
		if not player then
			return
		end
		if player.TeamColor ~= script.Parent.BrickColor then
			script.Parent.BrickColor = player.TeamColor
			local char  = t.Parent
			local torso = char:FindFirstChild 'UpperTorso' or char:FindFirstChild 'Torso'
			if torso then --> We should always check if these GameObjs exist before doing anything
				if t.Parent.Humanoid.Health <= 0 then
					return --> Let's make sure the player isn't already dead
				end
				--> Let's instantiate that player weld now...
				player_weld = Instance.new('Weld', script.Parent)
				player_weld.Part0  = torso
				player_weld.Part1  = script.Parent
				player_weld.Parent = script.Parent
				player_weld.Name   = 'Flag_Weld'
				
				--[!] You need to modify the C0/C1 (the coordinate frame positions of the joint)
				--[!] It's difficult for me to know how this is going to position as I don't know the orientation of your Flag object
				--[!] But if it's front surface is the horizontal aspect of the flag this should work:
				player_weld.C0 = CFrame.new(0, 0, 0.5) * CFrame.Angles(0, 0, -math.pi/8)
				
				--> Okay, now let's track if the player dies
				t.Parent.Humanoid.Died:connect(function ()
					--> Humanoids normally break joints on death, so let's just make sure that this joint still exists...
					if player_weld then
						player_weld:Destroy()
						player_weld = nil
					end
				end)
			end
		end
	end
end)
2 Likes

thank you so much guys.

Make the flag have a brickcolor value called teamcolor if this value matches with the player touched team then dont run the code