Flag not position properly

Hello, im trying to make CTF gamemode and I want it so that when the person dies while holding a flag, the flag spawns on them, but the flag doesnt change positions when I call its position.

Code:

local module = {}
local Players = game.Players
local Replicated = game.ReplicatedStorage
local VoteRemote = Replicated.Events.SendVote
local UpdateVote = Replicated.Events.IncrementVotes
local Flags = workspace.Flags
local FlagShower = Replicated.Assets.HasFlag

module.Gamemodes = {
	"tdm",
	"ctf",
	"domination",
	"hardpoint",
}

function VotingVisible(boolean)
	for _, Player in Players:GetPlayers() do
		Player.PlayerGui.MainUI.Voting.Visible = boolean
	end
end

function module:AutoTeam()
	
	for index, plr in ipairs(game.Players:GetPlayers()) do
		
		if index%2 == 0 then 
			plr.Team = game.Teams.Red
		else
			plr.Team = game.Teams.Blue
		end
		
	end
	
end

function module:StartVote()
	local SelectedPossibleGamemodes = {}
	
	repeat
		
		local randomGamemode = math.random(1, #module.Gamemodes)
		local gamemodeSelected = module.Gamemodes[randomGamemode]
		
		if table.find(SelectedPossibleGamemodes, gamemodeSelected) then else
			table.insert(SelectedPossibleGamemodes, gamemodeSelected)
			
		end
		wait()
	until #SelectedPossibleGamemodes >= 4
	
	VotingVisible(true)
	
	local gamemodevotables = {
		gamemode1votes = 0,
		gamemode2votes = 0,
		gamemode3votes = 0,
		gamemode4votes = 0
	}
	
	local playerswhohavevoted = {}
	
	local connection = VoteRemote.OnServerEvent:Connect(function(player, voteindex)
		if not table.find(playerswhohavevoted, player.Name) then
			
			local gamemodevoted = SelectedPossibleGamemodes[voteindex]
			gamemodevotables["gamemode"..tostring(voteindex).."votes"] = gamemodevotables["gamemode"..tostring(voteindex).."votes"]+1
			table.insert(playerswhohavevoted, player.Name)
			UpdateVote:FireAllClients(voteindex)
			
		end
	end)
	
	wait(20)
	
	connection:Disconnect()
	
	local highestgamemodevoted = 0

	for _, number in gamemodevotables do
		if number >= highestgamemodevoted then
			highestgamemodevoted = number
		end
	end
	VotingVisible(false)
	
	-- Converting dict to array
	
	local gmdvtablesarray = {}
	
	for _, v__ in gamemodevotables do
		table.insert(gmdvtablesarray, v__)
	end
	
	-- Find most voted
	local indextable = table.find(gmdvtablesarray, highestgamemodevoted)
	local MostVoted = SelectedPossibleGamemodes[indextable]
	
	--
	module.ctf()
	--module[MostVoted]()
	
end

module.tdm = function()
	workspace:SetAttribute("GameState", "tdm")
	
end

module.ctf = function()
	workspace:SetAttribute("GameState", "ctf")
	module:AutoTeam()
	
	local FlagConnections = {}
	
	for _, Flag in Flags:GetChildren() do
		Flag.Transparency = 0
		
		local flagconnection = Flag.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and Players:GetPlayerFromCharacter(hit.Parent) then
				local playertouched = Players:GetPlayerFromCharacter(hit.Parent)
				
				if playertouched.TeamColor == Flag.BrickColor and Flag.Transparency == 0 then 
					if playertouched:GetAttribute("isCapturingFlag") == true then
						workspace:SetAttribute(playertouched.Team.Name.."Score", workspace:GetAttribute(playertouched.Team.Name.."Score")+1)
						
						if playertouched.Character.Head:FindFirstChild("HasFlag") then
							playertouched.Character.Head.HasFlag:Destroy()
						end
						
						if playertouched.TeamColor == BrickColor.new("Navy blue") then
							workspace.Flags.FlagRed.Transparency = 0
						else
							workspace.Flags.FlagBlue.Transparency = 0
						end
						playertouched:SetAttribute("isCapturingFlag", false)
						
					end
				elseif playertouched.TeamColor ~= Flag.BrickColor and Flag.Transparency == 0 then
					
					Flag.Transparency = 1
					playertouched:SetAttribute("isCapturingFlag", true)
					
					local flagshow = FlagShower:Clone()
					flagshow.Parent = playertouched.Character.Head
					
					flagshow.TextLabel.TextColor = playertouched.TeamColor
					
				end
				
			end
		end)
		table.insert(FlagConnections, flagconnection)
	end
	
	local deathconnections = {}
	
	for _, playerCtf in Players:GetPlayers() do
		playerCtf:SetAttribute("Spawn", Flags["Flag"..playerCtf.Team.Name].CFrame)
		local pctfChar = playerCtf.Character
		
		local connectiondeath = pctfChar.Humanoid.Died:Connect(function()
			local position = pctfChar.HumanoidRootPart.Position + Vector3.new(0, 0.5, 0)
			
			if playerCtf:GetAttribute("isCapturingFlag") == true then
				playerCtf:SetAttribute("isCapturingFlag", false)
				
				local OldFlagPos = nil
				
				if playerCtf.TeamColor == BrickColor.new("Navy blue") then
					workspace.Flags.FlagRed.Position = position
					workspace.Flags.FlagRed.Transparency = 0
				else
					workspace.Flags.FlagBlue.Position = position
					workspace.Flags.FlagBlue.Transparency = 0
				end
				
			end
		end)	
		
		table.insert(deathconnections, connectiondeath)
		
	end
	
	repeat
		wait(1)
	until workspace:GetAttribute("RedScore") >= 5 or workspace:GetAttribute("BlueScore") >= 5
	
	for _, playersinmatchtoteam in Players:GetPlayers() do
		playersinmatchtoteam.Team = game.Teams.Lobby
		playersinmatchtoteam:SetAttribute("Spawn", workspace.SpawnLocation.CFrame)
	end
	
	for _, connections in deathconnections do connections:Disconnect() end
	for __, flagconn in FlagConnections do flagconn:Disconnect() end
	
end

module.domination = function()
	workspace:SetAttribute("GameState", "domination")
	
	
end

module.hardpoint = function()
	workspace:SetAttribute("GameState", "hardpoint")
	
	
end

return module

Could you show me just the part where it places the flag on the character?

Fixed by WAITING FOR THE CHARACTER TO RESPAWN BEFORE SETTING ATTRIBUTES.

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