Same local function on multiple instances

Hello i have been working on a script that making a player to a sheriff then gives him weapon and stuff, it gives him also a function for when he dies he will drop his hat and when a player picks it then he will become the new sheriff is it a way so i can give the new sheriff the same function for when he die instead of just writing the code again for him, because the new sheriff will be also able to drop his hat when he die.

`local sheriff = Survivors[math.random(1, #Survivors)]
if sheriff ~= murder then

			local sheriffvalue = Instance.new("BoolValue", sheriff.Character)
			sheriffvalue.Name = "SheriffValue"
			
			sheriff.PlayerGui.Role.Holder.RoleGui.RoleType.Text = "Sheriff"
			sheriff.PlayerGui.Role.Holder.RoleGui.RoleType.TextColor3 = Color3.fromRGB(0,0,255)
			sheriff.PlayerGui.Role.Enabled = true
			sheriffWeapon(sheriff, murder) 
			
			--- sheriff died ---
			sheriff.Character.Humanoid.Died:Connect(function()
				local SheriffHat = game.ServerStorage.SheriffThings.SheriffHat:Clone()
				SheriffHat.CFrame = sheriff.Character.PrimaryPart.CFrame + Vector3.new(0,0.1,0)
				SheriffHat.Parent = workspace
				
				--- new sheriff ---
				SheriffHat.Touched:Connect(function(hit)
					if hit.Parent:FindFirstChild("Humanoid") then
						if hit.Parent ~= murder.Character and hit.Parent ~= sheriff.Character then
							SheriffHat:Destroy()
							
							sheriff = game.Players:GetPlayerFromCharacter(hit.Parent)
							sheriffWeapon(sheriff, murder)
							print("new one "..sheriff.Character.Name)
						end
					end
				end)
			end)
		end`

the problem is when the new player gets the sheriff varible, the died function will not run on him.

The player that “picks up the SheriffHat”, never has its Character.Humanoid.Died connected.

You would need something along these lines of code:

local SpawnSheriffHat -- declare before defining the actual content of the function

local function AssignSheriff(player)
	sheriff = player

	local sheriffvalue = Instance.new("BoolValue", sheriff.Character)
	sheriffvalue.Name = "SheriffValue"
	
	local guiRole = sheriff.PlayerGui.Role
	guiRole.Holder.RoleGui.RoleType.Text = "Sheriff"
	guiRole.Holder.RoleGui.RoleType.TextColor3 = Color3.fromRGB(0,0,255)
	guiRole.Enabled = true

	sheriffWeapon(sheriff, murder)

	sheriff.Character.Humanoid.Died:Connect(function()
		SpawnSheriffHat( sheriff.Character.PrimaryPart.CFrame )
	end
end

SpawnSheriffHat = function(spawnAtCFrame)
	local sheriffHat = game.ServerStorage.SheriffThings.SheriffHat:Clone()
	sheriffHat.CFrame = spawnAtCFrame + Vector3.new(0,0.1,0)
	sheriffHat.Parent = workspace

	sheriffHat.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") then
			if hit.Parent ~= murder.Character and hit.Parent ~= sheriff.Character then
				sheriffHat:Destroy()
				AssignSheriff( game.Players:GetPlayerFromCharacter(hit.Parent) )
			end
		end
	end)
end

local newSheriff = Survivors[math.random(1, #Survivors)]
if newSheriff ~= murder then
	AssignSheriff( newSheriff )
end
1 Like