Help in my script

main idea: player who click the button will effect other(all) players the freeze, they will freeze

local script(button):
image

ServerScriptService:

my question is how can i make: the player who clicked the button will not be effected by the freeze

thanks for help :smiley:

can you post the code here? i cant really adjust it

local script(button):

script.Parent.MouseButton1Click:Connect(function()

game.ReplicatedStorage.Events.Freeze:FireServer(game.Players.LocalPlayer)
script.Parent.Parent.Visible = false

end)

serverScriptService:

game.Players.PlayerAdded:Connect(function(player)

game.ReplicatedStorage.Events.Freeze.OnServerEvent:Connect(function(clickName)

	if player:FindFirstChild("IsAfk").Value == false then
		print(clickName)
		if player.Name == clickName then
			
			player.Character.HumanoidRootPart.Anchored = false
			
		else	
			
			player.Character.HumanoidRootPart.Anchored = true
			wait(5) --Time utill unfreeze
			player.Character.HumanoidRootPart.Anchored = false			
			
		end
		
	else
		
		player.Character.HumanoidRootPart.Anchored = false

end
end)
end)

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.)

1 Like

you don’t really need to send game.Players.LocalPlayer since its a FireServer()

1 Like

but what should i do to make it work correct can you show example for my code

do you mean put first the remote stuff and then put playerAdded?

Why not just use a server script to detect if the player have press the button.

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.

yes its the in-game cash

what you wrote up there its gonna fix my prob?

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.

your code still effect the player who clicked the button, i want it not effect that player(who clicked)

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)
3 Likes

thank you work now i understand my problem

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.)