Hello! I want to make a mute command with a timer and datastore so it prevents players from leaving to bypass the mute.
Here is what I have so far, although I have no idea how to implement a timer, do I just used task.wait() or is it more complicated?
local TextChatService = game:GetService("TextChatService")
local function muteUserId(mutedUserId)
-- listen for future TextSources
TextChatService.DescendantAdded:Connect(function(child)
if child:IsA("TextSource") then
if child.UserId == mutedUserId then
child.CanSend = false
end
end
end)
-- mute any current TextSources
for _, child in TextChatService:GetDescendants() do
if child:IsA("TextSource") then
if child.UserId == mutedUserId then
child.CanSend = false
end
end
end
end
local function unmuteUserId(mutedUserId)
-- listen for future TextSources
TextChatService.DescendantAdded:Connect(function(child)
if child:IsA("TextSource") then
if child.UserId == mutedUserId then
child.CanSend = true
end
end
end)
-- mute any current TextSources
for _, child in TextChatService:GetDescendants() do
if child:IsA("TextSource") then
if child.UserId == mutedUserId then
child.CanSend = true
end
end
end
end
local muteremote = game:GetService("ReplicatedStorage").Remotes.Mute
muteremote.OnServerEvent:Connect(function(player, mode)
local char = player.Character
player:SetAttribute("Muted", false)
if mode == "Mute" and player:GetAttribute("Muted") == false then
player:SetAttribute("Muted", true)
muteUserId(player)
--timer here
unmuteUserId(player)
elseif mode == "Unmute" and player:GetAttribute("Muted") == true then
player:SetAttribute("Muted", false)
unmuteUserId(player)
end
end)
You can use a DateTime for the timer; just save the current value of the time in seconds of DateTime when that player gets muted.
To unmute them, compare the difference of the current value of DateTime with that value; if it’s greater than the mute duration in seconds, unmute them.
Hm, i’m a little confused because I want the executor of the command to insert a custom number value as how long the target will be muted for, do i use the same system? Sorry I wasnt being clear!
Yes, you can use a datetime for the system. In the other hand, you can use os.clock() to get an alpha time (now) and a delta time (unmute time) by simply using now + mute time.
In that way, the unmute time is correctly setup and can be stored in data stores.
You can do it by
DataStore:SetAsync(plr.UserId, (value) deltaTime)
And get the time by
DataStore:GetAsync(plr.UserId)
When player added.
Then check the deltatime in the value, and use unix time now subtracting it.
if (nowTime - deltaTime) < 1 then
Then unmute the player.
For muting, a while task.wait() function can be called and loop through a table containing all the players who are muted and their unmute time.