How do I fix my AFK script?

So I’m making a game and I’m trying to make a script that when a player types “!afk”
it will put a billboard GUI over there head.

heres the what I tried making

local CLONE = script.AFKGui:Clone()
game.Players.PlayerAdded:Connect(function(Player)
	local PlayerAFK = false

	Player.Chatted:Connect(function(Message)
		if Message == "!afk" then
			if PlayerAFK == false then
				PlayerAFK = true
				local ForceField = Instance.new("ForceField", Player.Character)
				 CLONE = Player.Character.Head
			else
				PlayerAFK = false
				Player.Character.ForceField:Destroy()
				Player.Character.Humanoid.DisplayName = Player.Name
			end
		end
	end)
end)

Edit: i tried putting this in lighting and server script server but it just says nothing and it wont appear

You’re setting a variable CLONE as the Player’s head. Instead, treat it like an object. Example: CLONE.Parent = Player.Character.Head
^ this also wouldn’t display an error as there is nothing internally wrong with this script, it just does two different actions.

2 Likes

So i tried putting it inside of StarterCharacterScripts and got this error ( AFKGui is not a valid member of Script “Workspace.Player1.AFKScript”)

  1. If this is in a localscript, it won’t show to all players in the server;
  2. Then this means the GUI you’re trying to reference isn’t inside of the script. Place the GUI inside of the script:
-----script
|
|
     --------GUI
|
|
......

its a script not a local script and its a billboard gui and I’m trying to put it above the users head but it doesn’t spit out an error and i put the billboard gui inside of the script

… huh? Can you please send a screenshot of the script inside the workspace and all of it’s children? I’m kind of lost here.

C1

This is the workspace, not StarterCharacterScripts. Either way try putting it in ServerScriptService and then using

local ui = script:WaitForChild("AFKGui", 10)
local CLONE = ui:Clone()

If this doesn’t work, another person will have to help you.

C2 now it spits out an error

Why are you passing the 10 through the WaitForChild()? He’s trying to just get the gui. I believe the 10 is causing the error.

It times out after 10 seconds, causing it not the have an infinite yield (if the object is not found ever).

I think the reason is that the script is the Server Script Service is not inside of the GUI, I think you are trying to find something that isn’t in the scripts “kids”.

In the workspace, it shows the UI inside of the script, and they’re calling script.AFKGui, why wouldn’t this be inside script?

1 Like

Have you set the parent of the clone?

I wouldn’t recommend putting this in StarterCharacterScripts. Scripts within StarterCharacterScripts will not reappear in the character if the character is destroyed (meaning the player dies and has to respawn).

Also, I wouldn’t, but the BillboardGUI inside of the script. I’d put the script in the BillBoardGUI, then put the GUI in something like ServerStorage or StarterGui. You’d have to change your CLONE variable to script.Parent.

Finally, make sure to set the Adornee property to the character’s head. Setting the parent to the head I don’t think is enough.