Hello, I am making an AFK button for my game and in the Main script where it teleports player to the game, but if the player is AFK then it won’t teleport. However, it is not checking if it is OFF or ON. It just goes right through the if statement even though it is ON. Can someone help? Thanks. Here is my current code:
for i,player in pairs(plrs) do
if player then
if player.PlayerGui.AFK.TextLabel.Text == "OFF" then
local char = player.Character
if char then
char:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawns[1].CFrame + Vector3.new(0,2,0)
table.remove(AvailableSpawns,1)
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = char
else
if not player then
table.remove(plrs,i)
end
end
elseif player.PlayerGui.AFK.TextLabel.Text ~= "OFF" then
break
end
end
end
Also if you were planning to say it only checks once, then you would be wrong as this is only part of the script and it waits like 20 seconds before it does this part of code. Giving the player plenty of time to click OFF or ON.
is this a server script or a local script? If it’s a server script then that is your issue as you are directly calling the ui from player.PlayerGui. The text only changes on the client side, meaning that on the server side it’ll always be ON even if it is off on the client side.
Then that is your issue. A way to fix this is to use a remote event to do the actions on the server and do the afk calculations on a local script. Could you show your local script that updates the text label text?
local RS = game.ReplicatedStorage
local SS = game.ServerStorage
local Maps = SS:WaitForChild("Maps")
local Status = RS:WaitForChild("Status")
local intermissionLength = 15
local Timer = game.ReplicatedStorage.Timer
local data = require(game.ReplicatedStorage["Questions/Answers"])
local highestNumber = 0
local chosen = nil
while true do
--Status.Value = "Waiting for enough players! (1/2)"
--repeat task.wait() until game.Players.NumPlayers >=2
Status.Value = "Intermission:"
task.wait(3)
Status.Value = "15"
for i=1,intermissionLength do
task.wait(1)
local new = tonumber(Status.Value)-1
Status.Value = tostring(new)
end
--END OF INTERMISSION
local plrs = {}
for i,player in pairs(game.Players:GetPlayers()) do
if player then
table.insert(plrs,player) --add plr
end
end
local padVotes = {
["Construction"] = tonumber(workspace.Lobby.Construction.SurfaceGui.TextLabel.Text),
["Radioactive"] = tonumber(workspace.Lobby.Radioactive.SurfaceGui.TextLabel.Text),
["City"] = tonumber(workspace.Lobby.City.SurfaceGui.TextLabel.Text)
}
for i, v in pairs(padVotes) do
local number = v
if number > highestNumber then
highestNumber = number
chosen = i
end
end
local ChosenMap = tostring(chosen)
task.wait(.3)
if ChosenMap == nil or ChosenMap == "0" or ChosenMap == "nil" then
local AvailableMaps = Maps:GetChildren()
ChosenMap = tostring(AvailableMaps[math.random(1,#AvailableMaps)])
Status.Value = tostring(ChosenMap).. " has been chosen!"
else
Status.Value = tostring(ChosenMap).. " has been chosen!"
end
task.wait(2)
Status.Value = "Game starting!"
task.wait(2)
local ClonedMap = game.ServerStorage.Maps:FindFirstChild(ChosenMap):Clone()
if ClonedMap ~= nil then
if ClonedMap == "Radioactive" then
ClonedMap:PivotTo(CFrame.new(490.263, 79.457, -4.343) * CFrame.Angles(0, 0, math.pi))
elseif ClonedMap == "Construction" then
ClonedMap:PivotTo(CFrame.new(490.263, 35.957, -4.343))
elseif ClonedMap == "City" then
ClonedMap:PivotTo(CFrame.new(490.263, 35.957, -4.343))
end
end
ClonedMap.Parent = workspace
local SpawnPoints = game.ServerStorage.Maps[ChosenMap]:FindFirstChild("SpawnPoints")
if not SpawnPoints then
warn("No spawnpoints found")
end
--END OF CHOOSING MAP
local AvailableSpawns = SpawnPoints:GetChildren()
for i,player in pairs(plrs) do
if player then
if player.PlayerGui.AFK.TextLabel.Text == "OFF" then
local char = player.Character
if char then
char:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawns[1].CFrame + Vector3.new(0,2,0)
table.remove(AvailableSpawns,1)
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = char
else
if not player then
table.remove(plrs,i)
end
end
elseif player.PlayerGui.AFK.TextLabel.Text ~= "OFF" then
break
end
end
end
--END OF TELEPORTING PLAYERS
Status.Value = "Get ready to play!"
task.wait(2)
Timer.Value = 15
repeat
game.ReplicatedStorage.SafeAnswer:FireAllClients()
local questionnum = math.random(#data)
local question = data[questionnum].question
Status.Value = question
Timer.Value = 15
for i=1,15 do
for i,player in pairs(plrs) do
if player then
local character = player.Character
if not character then
print("Player left the game")
table.remove(plrs,i)
elseif character:FindFirstChild("GameTag") then
print(player.Name.. " is still in the game!")
else
table.remove(plrs,i)
end
else table.remove(plrs,i)
print(player.Name.. " has been removed")
end
end
task.wait(1)
local new = Timer.Value-1
Timer.Value = new
end
ClonedMap.Lava.Size += Vector3.new(0,10,0)
for i,player in pairs(plrs) do
if player then
local character = player.Character
if not character then
print("Player left the game")
table.remove(plrs,i)
elseif character:FindFirstChild("GameTag") then
print(player.Name.. " is still in the game!")
else
table.remove(plrs,i)
end
else table.remove(plrs,i)
print(player.Name.. " has been removed")
end
end
until #plrs <= 1
if #plrs == 1 then
if plrs[1].Character:FindFirstChild("GameTag") then
Status.Value = "The winner is: " ..plrs[1].Name.. "!"
plrs[1].leaderstats.Wins.Value += 1
Timer.Value = 180
end
elseif #plrs <= 0 then
Status.Value = "Nobody won!"
Timer.Value = 180
end
task.wait(2)
Status.Value = "End of game!"
task.wait(2)
for i,player in pairs(game.Players:GetPlayers()) do
local charac = player.Character
if not charac then
print("Player does not have character!")
elseif charac then
if charac:FindFirstChild("GameTag") then
charac.GameTag:Destroy()
player:LoadCharacter()
end
end
end
for i,v in pairs(workspace.StepsFolder:GetChildren()) do
if v.Name == "Step" then
v:Destroy()
end
end
ClonedMap:Destroy()
workspace.Lobby.Construction.SurfaceGui.TextLabel.Text = "0"
workspace.Lobby.Radioactive.SurfaceGui.TextLabel.Text = "0"
workspace.Lobby.City.SurfaceGui.TextLabel.Text = "0"
Timer.Value = 180
task.wait(3)
--END OF GAME ENDING
end
i think this should work, replace ur for loop with this
make sure to add a “RemoteEvents” folder into replicated storage with a remoteevent called “Switch”
SERVER SCRIPT: (replace ur for loop of the players with this)
game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild("Switch").OnServerEvent:Connect(function(player, on)
if not on then
char:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawns[1].CFrame + Vector3.new(0,2,0)
table.remove(AvailableSpawns,1)
local GameTag = Instance.new("BoolValue")
GameTag.Name = "GameTag"
GameTag.Parent = char
end
end)
replace localscript with this:
-- edit: kind of cleaned tween up a bit
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(.8, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local anim = TweenService:Create(script.Parent.Parent.TextLabel, tweenInfo, {TextTransparency = 0})
local anim2 = TweenService:Create(script.Parent.Parent.TextLabel.UIStroke, tweenInfo, {Transparency = 0})
local event = game.ReplicatedStorage:WaitForChild("RemoteEvents"):WaitForChild('Switch')
script.Parent.MouseEnter:Connect(function()
script.Parent.Parent.TextLabel:TweenPosition(
UDim2.new(0.08, 0, 0.665, 0),
"InOut",
"Quad",
1,
true
)
anim2:Play()
anim:Play()
end)
script.Parent.MouseLeave:Connect(function()
script.Parent.Parent.TextLabel:TweenPosition(
UDim2.new(0.015, 0, 0.665, 0),
"InOut",
"Quad",
1,
true
)
anim2:Play()
anim:Play()
end)
script.Parent.Parent.MainButton.MouseButton1Click:Connect(function()
if script.Parent.Parent.TextLabel.Text == "OFF" then
event:FireServer(true)
script.Parent.Parent.TextLabel.Text = "ON"
elseif script.Parent.Parent.TextLabel.Text == "ON" then
event:FireServer(false)
script.Parent.Parent.TextLabel.Text = "OFF"
end
end)