Need help Implementing an AFK setting

So I’ve been scripting for around a week now and I wanted to implement an afk setting which when you turn on would make you sit out and you can’t play until you turn it off. So the code for this is:

Sorry for the colours, I was just messing around with some themes. So the afk is in the players>Settings>AFK and It’s a boolvalue which by default is set to false and when I manually change it, nothing happens and even when I script it too change nothing happens.

So what I’m trying to figure out is how to implement the afk setting? Right now I have it so check if the players afk boolvalue is not true. I’m guessing there code to check if it has updated. Not sure please help.

1 Like

I’m a bit confused as to what you are asking, I’m going to assume you mean how they could toggle it. You could use a TextButton, ImageButton, a part that teleports them with a Touched function or a command. Many methods are possible (if that is what you were asking). A basic example if a command would be:

game.Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(chat)
        if chat == "/AFK" then
            player.AFK.Value = true
            player.Character.HumanoidRootPart.CFrame = --Teleport them to the AFK area.
            --Maybe add some Gui that lets them know they went AFK.
        elseif chat == "/BFB" then
            player.AFK.Value = false
            player.Character.HumanoidRootPart.CFrame = --Teleport them back in.
        end
    end)
end)

I don’t advise doing it via commands, a TextButton would be recommended but it’s just the easiest example I can think of. But again I am not sure if that is what you were asking for.

So I did make temporary button that when pressed the AFK.Value would change and the value did change when I tested it, but the game still carried on as if I was never afk. If you get what I mean. So by default the value is set to false and when the button was pressed it changed the value to true but it carried on with the main script.

If it’s a button on a GUI, then it is being fired from a LocalScript I am guessing. Did you use a RemoteEvent to carry out the necessary actions from the server side?

I did not use a RemoteEvent but I will try it. I used a normal local script; image

If you run code on a LocalScript, the outcome will only be visible to the LocalPlayer. For example if you make a part invisible using a LocalScript, other players would still see the part; meaning server scripts will not be able to detect the change. In your case, you can do something like this for your LocalScript:

local Remote = game:GetService("ReplicatedStorage"):WaitForChild("AFKRemote") --Add the RemoteEvent "AFKRemote" to "game.ReplicatedStorage".
script.Parent.MouseButton1Click:Connect(function()
    Remote:FireServer()
end)

This will fire the ServerEvent. Now have a script in ServerScriptService that looks something like this:

local Remote = game:GetService("ReplicatedStorage"):WaitForChild("AFKRemote")
Remote.OnServerEvent:Connect(function(player)
    player.AFK.Value = false
end)

Note that the first parameter of the OnServerEvent function will always be the player who fired the event. But on the LocalScript, the first parameter of the :FireServer() will be sent to the second parameter of the ServerScript.

Yeah so the Afk value does change but the main script still adds me to the CurrentRound Folder. So I think there may be an error the main script rather than the Values.

This is the part in the main script that does the Checks the afk value.

Based on your original script, it seems to create a BoolValue every time the player’s AFK value is false? So every “intermission” a new value will be created? Or maybe I’m just lost.

So this script is what creates the AFK BoolValue: image

And it’s set to true when a player is added. And when I start the game with it being true it will put me in the game. But if I set the true to false it will not add me to the game.

Every Intermission It will check the afk value to see whether you are afk or not.

I see. But in your main script I see that the instance val is a new instance every time intermission runs?

So the val creates a StringValue with the players name and add them to a round folder. Every Intermission.

Oh my bad, I did not read the lines below. Within that loop could this be added? Something like:

for i = 1, #player do
    if player[i].AFK.Value ~= true then
    
        local val = Instance.new("StringValue")
        val.Name = player[i].Name
        val.Parent = game.Workspace.MainFrame.InRound
        player[i].Character.HumanoidRootPart.CFrame = --Teleport outside of the game area?
    end
end

Or add it to the part where the game starts?

So the problem I’m having is that this script doesn’t realise that the Value of the AFK has been changed.

Add this to the RemoteEvent handler and use your original code for this.


local event =  -- define event
event.OnServerEvent:Connect(function(player)
	if player.AFK.Value == true then
		local val = Instance.new("StringValue")
		val.Name = player.Name -- edited here
		val.Parent = game.Workspace.MainFrame.InRound
		player.AFK.Value = false
		print("Player no longer AFK")
	else
		game.Workspace.MainFrame.InRound:FindFirstChild(player.Name):Destroy()
		player.AFK.Value = true
		print("Player currently AFK")
	end	
end)

image

I get an Error; Incomplete Statement: Expected assignment or a function call.

I edited the previous post check again.
Edit: Check again i didn’t see the double equal sign. @TheDCraft

1 Like

Same error. Incomplete Statement: Expected assignment or a function call.

Thank you It works :smiley: Your a legend.