Hello! I am making a AFK System to have a overhead AFK text over, and It is based on a value inside the player, a boolvalue called AFK to be exact. And the system itself works but it errors like 2 times in the output with AFK is not a valid member of Player "Players.DaffyDavinko" - Client - AFK:32 and also the script is in StarterPlayerScripts.Please reply if you know what should I try and do! (some of the script I’ve found in another post and re-used and edited to make it work with my system)
-- >> Services << --
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
-- >> Variables << --
local LocalPlayer = Players.LocalPlayer
local Idle = false
local Time = 0
-- >> Events << --
UserInputService.InputEnded:Connect(function()
Idle = true
while wait(1) do
Time += 1
if not Time then
break
end
-------------------------------
if Time >= 240 then
LocalPlayer:FindFirstChild("AFK").Value = true
break
end
end
end)
UserInputService.InputBegan:Connect(function()
Idle = false
end)
RunService.Heartbeat:Connect(function()
if game.Players.LocalPlayer.AFK.Value == true then
game.Players.LocalPlayer.Character.Head.Rank.AFK.Visible = true
elseif game.Players.LocalPlayer.AFK.Value == false then
game.Players.LocalPlayer.Character.Head.Rank.AFK.Visible = false
end
end)
You should use if LocalPlayer:FindFirstChild(“AFK”) then to see if the instance exists before trying to get the value.
A more efficient way is to wait for the child and wait for the value to change LocalPlayer:WaitForChild(“AFK”):GetPropertyChangedSignal(“Value”):Connect(function()
It only appears at this line, according to the error line you have provided. Have you made sure this is the correct path? Also, make sure to add code that prevents the script from running until all the nessesary variables are loaded. This can be done with :WaitForChild().
If you wanted to place it before the heartbeat loop, you could still do LocalPlayer.Character:WaitForChild(“Head”):WaitForChild(“Rank”):WaitForChild(“AFK”), but yeah thats one way
Honestly repeat wait() until is better if you’re waiting for multiple things since it does it all in one go without any hassle. Once that code is done, you can assign other variables as the parents/ancestors of the lowest descendant without needing to use :WaitForChild().
WaitForChild only checks if the instance has been added when .ChildAdded is fired but yeah, just preference
You would still need to do repeat wait() until game.Players.LocalPlayer.Character:FindFirstChild(“Head”):FindFirstChild(“Rank”):FindFirstChild(“AFK”) or it would error
I recommend just using :WaitForChild() as it is more efficient and is basically the same length as typing :FindFirstChild()
local AFK = LocalPlayer:WaitForChild("AFK")
AFK:GetPropertyChangedSignal("Value"):Connect(function()
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
Character:WaitForChild("Head"):WaitForChild("Rank"):WaitForChild("AFK").Visible = AFK.Value
end)
If you would want the UI to appear for other clients, I recommend using a RemoteEvent and let the server toggle the UI.
Since the error only popped up twice in a heartbeat loop. My best guess is that the AFK value is in the Player instance but the AFK value hadn’t been created and parented for the first two frames
The AFK instance had not already been created because it errored twice in a heartbeat loop. Since it only errored twice in a heartbeat loop, it is best to assume that it was available after the two frames. It is impossible to stop the testing in 3 frames (50 milliseconds or 0.05 seconds).