Sign in to follow this  
Lynch

Custom mute system

Recommended Posts

The code is written to mute the player upon spamming, which works fine, except that if a player quits the game, changes his nick, and joins again, he does get muted, but won't get unmuted. Also, if the player quits after being muted, and waits some time before joining back, he'll be forever muted again and won't get unmuted. Can't think of a way to fix this. Any help would be appreciated.

mutedPlayerSerials = {}
interval = 20000

addEvent("mutePlayer", true)
addEventHandler("mutePlayer", root, function()
	local mutedPlayer = getPlayerName(source)
	local serial = getPlayerSerial(source)

	outputChatBox(mutedPlayer.." has been muted by console for spamming.", root, 255, 100, 100)
	table.insert(mutedPlayerSerials, serial)
	setPlayerMuted(source, true)

	setTimer(function()
		local serial = nil
		mutedPlayerSerials = {}
		local player = getPlayerFromName(mutedPlayer)
		if player then
			setPlayerMuted(player, false)
			outputChatBox(mutedPlayer.." has been unmuted by console.", root, 100, 255, 100)
		end
	end, interval, 1)

end)

mutedPlayer = false

addEventHandler("onPlayerJoin", root, function()
	local serial = getPlayerSerial(source)

	for _, v in pairs(mutedPlayerSerials) do
		if serial == v then
			mutedPlayer = true
			break
		end
	end

	if mutedPlayer == true then
		local mutedPlayerName = getPlayerName(source)
		local mutedPlayer = getPlayerFromName(mutedPlayerName)
		setPlayerMuted(mutedPlayer, true)
		outputChatBox(mutedPlayerName.." has been muted by console.", root, 255, 100, 100)
	end
end)

 

Share this post


Link to post
muteTimers = {}
interval = 20000

function findPlayerBySerial(serial)
	for _, player in ipairs(getElementsByType("player")) do
		if getPlayerSerial(player) == serial then
			return player
		end
	end
	return false
end

addEvent("mutePlayer", true)
addEventHandler("mutePlayer", root, function()
	local name = getPlayerName(source)
	local serial = getPlayerSerial(source)

	outputChatBox(name.." has been muted by console for spamming.", root, 255, 100, 100)
	setPlayerMuted(source, true)

	muteTimers[serial] = setTimer(function()
		local player = findPlayerBySerial(serial)
		if player then
			setPlayerMuted(player, false)
			local name = getPlayerName(player)
			outputChatBox(name.." has been unmuted by console.", root, 100, 255, 100)
		end
	end, interval, 1)
end)

addEventHandler("onPlayerJoin", root, function()
	local serial = getPlayerSerial(source)
	if isTimer(muteTimers[serial]) then
		local name = getPlayerName(source)
		outputChatBox(name.." has been muted by console.", root, 255, 100, 100)
		setPlayerMuted(source, true)
	end
end)

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.