#Afk event wont work

73 messages · Page 1 of 1 (latest)

brazen tree
#

Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ForceField = Instance.new("ForceField")

local FocusedEvent = ReplicatedStorage:WaitForChild("Focused")
local UnFocusedEvent = ReplicatedStorage:WaitForChild("Unfocused")

local function createforcefiled(player)
    local Forcefield = Instance.new("ForceField")
    Forcefield.Visible = true
    Forcefield.Parent = player.Character
    for _, v in player.Character:GetDescendants() do
        if v:IsA("MeshPart") then
            v.Material = Enum.Material.ForceField
        end
    end
end

local function removeforcefield(player)
    player.Character.Forcefield:Destory()
    for _, v in player.Character:GetDescendants() do
        if v:IsA("MeshPart") then
            v.Material = Enum.Material.Plastic
        end
    end
end
FocusedEvent.OnServerEvent:Connect(function(player)
    removeforcefield(player)
end)

UnFocusedEvent.OnServerEvent:Connect(function(player)
    createforcefiled(player)
end)

(that script is in server script service)
(The next script is in StarterPlayerScripts)

local InputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local FocusedEvent = ReplicatedStorage:WaitForChild("Focused")
local UnFocusedEvent = ReplicatedStorage:WaitForChild("Unfocused")

InputService.WindowFocused:Connect(function()
    FocusedEvent:FireServer()
end)

InputService.WindowFocusReleased:Connect(function()
    UnFocusedEvent:FireServer()
end)

The error:

Infinite yield possible on 'ReplicatedStorage:WaitForChild("Focused")'```
plush oak
brazen tree
ancient lightBOT
#

studio** You are now Level 5! **studio

brazen tree
#

wait look at this error @plush oak

#

Whats wrong w line 21

plush oak
#

wait 2 secs

#

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local FocusedEvent = ReplicatedStorage:WaitForChild("Focused")
local UnFocusedEvent = ReplicatedStorage:WaitForChild("Unfocused")

local function createForceField(player)
if player.Character and player.Character:FindFirstChild("Humanoid") then
local forceField = Instance.new("ForceField")
forceField.Parent = player.Character
end
end

local function removeForceField(player)
if player.Character then
local forceField = player.Character:FindFirstChild("ForceField")
if forceField then
forceField:Destroy()
end
end
end

-- AFK detection
local function monitorAFK(player)
local isAFK = false
local inactivityTime = 0
local afkThreshold = 5 -- Time in seconds to consider as AFK

while true do
    wait(1) 
    if isAFK then
        inactivityTime = inactivityTime + 1
        if inactivityTime >= afkThreshold then
            createForceField(player)
        end
    else
        inactivityTime = 0
    end
end

end

FocusedEvent.OnServerEvent:Connect(function(player)
removeForceField(player)
isAFK = false
end)

UnFocusedEvent.OnServerEvent:Connect(function(player)
isAFK = true
end)

Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
monitorAFK(player)
end)
end)

#

let me know if it works @brazen tree

#

and put this in starterplayerscripts with local script

#

local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local FocusedEvent = ReplicatedStorage:WaitForChild("Focused")
local UnFocusedEvent = ReplicatedStorage:WaitForChild("Unfocused")

UserInputService.WindowFocused:Connect(function()
FocusedEvent:FireServer()
end)

UserInputService.WindowFocusReleased:Connect(function()
UnFocusedEvent:FireServer()
end)

plush oak
# brazen tree Whats wrong w line 21

that can mean that forcefield isn't parented to the player's character
or
the force field is trying to access the player's character before is loaded

ancient lightBOT
#

studio** You are now Level 1! **studio

brazen tree
#

alr

#

snd

plush oak
#

y it works

brazen tree
plush oak
brazen tree
#

not aroudn the character

#

so

#

@plush oak

plush oak
#

don't you mean to be on the char?

#

like how

#

can you show me

brazen tree
brazen tree
plush oak
#

let me see if i can

brazen tree
#

k*

#

@plush oak Anything yet?

plush oak
plush oak
plush oak
brazen tree
#

no

#

it works on me

#

its just i autoload in with it on even when im not afk

plush oak
#

for me no? what

#

send me one last time the script

#

srry

brazen tree
#

did u read that there is 2 scripts in that

#

and did u put em in the right place?

plush oak
ancient lightBOT
#

studio** You are now Level 2! **studio

plush oak
#

wdym

brazen tree
#

Where in the middle i said:

plush oak
#

ye

brazen tree
#

(that script is in server script service)
(The next script is in StarterPlayerScripts)

and at the end

The error:

Infinite yield possible on 'ReplicatedStorage:WaitForChild("Focused")

#

so u put em in the right place for suer?

plush oak
#

ye

brazen tree
#

wierd..

plush oak
#

do yu have another texture for force field

brazen tree
#

wdym

brazen tree
plush oak
#

for me is the default

brazen tree
#

wait ignore what im about to send

#

wait actually

#

dw

#

i fixed it i think

#

gimme 1m

#

local AFKStatus = {}
local AFKEvent = game.ReplicatedStorage:FindFirstChild("AFK")

--[ FUNCTIONS ]--

if not AFKEvent then
AFKEvent = Instance.new("RemoteEvent")
AFKEvent.Parent = game.ReplicatedStorage
AFKEvent.Name = "AFK"
end

local updateAFK = function(player,enable)

local char = player.Character -- character
local parts = char:GetDescendants() -- get children of the character

if enable then -- if afk
    for i,v in pairs(parts) do
        if v:IsA("BasePart") then
            v.Material = "ForceField" -- setting parts to forcefield
        end
    end
else -- if not afk
    for i,v in pairs(parts) do
        if v:IsA("BasePart") then
            v.Material = "Plastic" -- setting parts to plastic (original)
        end
    end
end

end

AFKEvent.OnServerEvent:Connect(function(player,enable) -- on server event (enable)
AFKStatus[player] = enable
updateAFK(player,enable)
end)

#

local InputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local FocusedEvent = ReplicatedStorage:WaitForChild("Focused")
local UnFocusedEvent = ReplicatedStorage:WaitForChild("Unfocused")

local function createforcefield(player)
local ForceField = Instance.new("ForceField")
ForceField.Visible = true
ForceField.Parent = player.Character
for _, v in player.Character:GetDescendants() do
if v:IsA("MeshPart") then
v.Material = Enum.Material.ForceField
end
end
end

local function removeforcefield(player)
player.Character.ForceField:Destory()
for _, v in player.Character:GetDescendants() do
if v:IsA("MeshPart") then
v.Material = Enum.Material.Plastic
end
end
end
FocusedEvent.OnServerEvent:Connect(function(player)
removeforcefield(player)
end)

UnFocusedEvent.OnServerEvent:Connect(function(player)
createforcefield(player)
end)

#

i fixed it

#

dw

#

@plush oak

plush oak
#

for me is still default

#

i have the studio bugged

brazen tree
#

i switxched a script round and got some edits