The issue here is you are creating an âif elseâ for every player because it is inside the PlayerAdded. Put the Remote stuff outside the PlayerAdded and it should work as you expect it to. (But also make sure you use the âclickNameâ as the player to check the IsAfk value.)
game.ReplicatedStorage.Events.Freeze.OnServerEvent:Connect(function(LocalPlayer)
local isAFK = LocalPlayer:FindFirstChild("IsAfk")
if isAFK then
LocalPlayer.Character.HumanoidRootPart.Anchored = not isAFK.Value
if not isAFK.Value then
wait(3)
LocalPlayer.Character.HumanoidRootPart.Anchored = false
end
else
warn("AFK Value missing from: ", LocalPlayer.Name)
end
end)
i want to make shop button that player can purchase its called âFreeze All Playersâ for amount of cash that they get and for that i need it to freeze all the players but the player who bought it and clicked the button it will not effect
i want to make shop button that player can purchase its called âFreeze All Playersâ for amount of cash that they get and for that i need it to freeze all the players but the player who bought it and clicked the button it will not effect
the isAfk is argument when player in the lobby âAFKâ it will not effect him
And is this âcashâ button using In-game cash or Robux? I ask becuase if using Robux, there is a different function you can use, using PromptProductPurchase, but if this is in-game cash then your current method needs to use a loop to get all current players and checking all the players. PlayerAdded is on the right track but not exactly what you want to do.
Not entirely. I was not sure you wanted it to affect all players. You will need to do a â:GetPlayers()â loop on the Players service and then check if each player has afk off and if the player is not the local player who called the remote.
change if player.Name == clickName then to if player == clickName then
The returned variable is the actual player and not his name
Also your code can be improved, this is not the most efficient
Yes I know, I was in the middle of re-working the script to fit what you are looking for:
game.ReplicatedStorage.Events.Freeze.OnServerEvent:Connect(function(LocalPlayer)
for _, player in pairs(game.Players:GetPlayers()) do
local isAFK = player:FindFirstChild("IsAfk")
if isAFK then
if not isAFK.Value and player ~= LocalPlayer then
player.Character.HumanoidRootPart.Anchored = true
delay(3,function()
player.Character.HumanoidRootPart.Anchored = false
end)
end
else
warn("AFK Value missing from: ", player.Name)
end
end
end)
You had a misconception of :FindFirstChild here - the IsAfk is value should always supposedly be there, meaning you shouldnât use :FindFirstChild. Yet you did, and you did it incorrectly, because you used :FindFirstChild and tried to index it.
What :FindFirstChild does is that it checks if there is a child / descendant (Depends if you pass the #2 parameter as true) which itâs name is argument #1, but if there isnât, it wonât propagate the error, but instead return nil.
And so, you should check whether what :FindFirstChild returned, because if you directly index what is returned by it, and it is nil, then itâll propagate the error. (If it is not in a pcall.)