need help with script & CurTime

Hello, i’m new to the forums and in need of someones help.

I need to set up a timer/CurTime for my script.

You can guess what it does by looking at it.

The script needs to have an auto kick timer if they do not initiate “removeafk” i have tried a few things to do with Curtime & simple.timer but i’am hopeless and new to scripting.

What the script looks like:


function chatCommand( ply, text, public )
    if (string.sub(text, 1, 4) == "/afk") then
	local afk = ply
	afk:GodEnable()
    afk:SetRenderMode(RENDERMODE_TRANSALPHA)
	afk:SetColor(Color(0, 0, 0, 59))
	afk:Freeze( true )
	afk:SetCollisionGroup(COLLISION_GROUP_DEBRIS)
	afk:ConCommand("afkmenu")
  end
  end
  
  hook.Add( "PlayerSay", "chatCommand", chatCommand )
  
 concommand.Add( "removeafk", function( ply, cmd, args )
	local rafk = ply
	rafk:GodDisable()
	rafk:SetColor(Color(255, 255, 255, 255))
	rafk:Freeze( false )
	rafk:SetCollisionGroup(COLLISION_GROUP_NONE)
	end )

I also need an example of a VGUI element that counts down the time on the frame i made.
kinda like how an motd counts down before you can close it but in this case it would only be counting down the time before it kicks you.

In chatCommand, make a timer with

timer.Create and remove it in removeafk’s function. Ex.


-- In chatCommand
timer.Create("AFKTimer" .. ply:SteamID(), 120, 1, function() if (ply:IsValid()) then ply:Kick("AFK") end end)

-- In concommand
timer.Remove("AFKTimer" .. ply:SteamID())

Also, a few things with your code: you don’t have to make another scope variable for the player (afk and rafk for example), just use ply if it’s provided. Also, don’t use substring comparrisons for command or else “/afkblahblah” will work, as well. Lastly, make chatCommand a local function since you’re only using it in the hook.

I have a habit of making custom named functions and because this is in DarkRP modules i couldn’t use ply twice so i just renamed the function to afk & rafk or else the server comes up with errors.

also i could not make it local function because the way godmode works and freeze are called server side.

The script itself works fine how it is. just needed a kick timer. But THANK YOU! for the help :slight_smile:

(Probably a much simpler way to make this script but yet again. “im a noob with good intentions” )

If anyone could help with the VGUI countdown would be appreciated <3

You can make it a local function since you’re only using it in the hook afterward, and your already defining ply as the player in the function. If you’re getting errors from either, you are doing something wrong.

You are giving the player alot of control over themselves by allowing instant toggle of invisibility and godmode, and I imagine players might abuse it in DarkRP for things like evading hostile encounters with other players.

But that aside, here’s a vgui example that is based on the code already provided:



local function AFKMenu(timeout)
	
	local window = vgui.Create("DFrame")
	window:SetSize(140, 100)
	window:SetTitle("AFK time until kick")
	window:Center()
	window:SetDraggable(false) -- Prevents dragging the window around
	window:SetBackgroundBlur(true) -- Blurs out the background, delete the line if undesired
	window:ShowCloseButton(false) -- Don't let the client close the window, we do that with the button
	window:MakePopup() -- Changes focus to the window so the client can interact with it
	window.OldPaint = window.Paint -- Save the default paint function
	
	local clr = Color(255, 0, 0) -- Color of the countdown

	function window:Paint(w, h)
		self:OldPaint(w, h) -- Call the default paint function
		draw.SimpleText(math.floor(timeout-CurTime()), "DermaLarge", w/2, h/2, clr, 1, 1)
	end
	
	local button = vgui.Create("DButton", window) -- Sets window as parent
	button:SetTall(20) -- If this isn't, specified, it'll use default button height, so this is just for when wanting a custom height of the button
	button:DockMargin(20, 0, 20, 0) -- Adds margins to the sides of the button so it doesn't fill out all the way to the sides
	button:Dock(BOTTOM) -- We want the button at the bottom of the window
	button:SetText("I'm back!")
	button.DoClick = function()
		RunConsoleCommand("removeafk")
		window:Remove()
	end
	
	
end

concommand.Add("afkmenu", function(ply, cmd, args)
	local timeout = tonumber(args[1])
	if not timeout then return end -- The first argument wasn't a number, this should only happen if the client tries to run afkmenu in console without a number as an argument
	AFKMenu(timeout)
end)

-- Server code for running the command:

local timer = 120 -- This should be the number you also use in the timer

-- Inside the chat command code:
local kicktime = CurTime()+timer
ply:ConCommand("afkmenu "..math.floor(kicktime))