Loop through players doesn't work

You can write your topic however you want, but you need to answer these questions:
I made a text label that makes the thing the player puts in there happen, for example explode every player or kill every player

  1. What is the issue? Include screenshots / videos if possible!
    Everything works besides that the main thing doesn’t, it doesn’t create explosion or anything else from the list

  2. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried debugging it but the script makes it only untill print in any case and doesn’t go through players

-- This is a local script 
-- Services
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")


-- Items
local Sign = script.Parent
local gui = Sign.SurfaceGui
local frame = gui.Frame
local textBox = frame.TextBox
local Remote = repStorage:WaitForChild("RemoteEvent")
-- Possibs
local Possibilities = {"explosion", "kill", "platformstand"}

--TextBoxScript
textBox.FocusLost:Connect(function()
	local text = textBox.Text
	local loweredText = string.lower(text)
	for i,Possib in pairs(Possibilities) do
		if loweredText == Possib then
			print("Found " ..Possib)
			Remote:FireServer(Possib)
		else
			print("Not Found")
			
		end
	end
end)
-- This is a server script 
local repStorage = game:GetService("ReplicatedStorage")

local playersConnected = game:GetService("Players")

local players = playersConnected:GetPlayers()

local Possibilities = {"explosion", "kill", "platformstand"}

local remote = repStorage:WaitForChild("RemoteEvent")

local Possibilities = {"explosion", "kill", "platformstand"}

remote.OnServerEvent:Connect(function(Player, Possib)
	if Possib == Possibilities[1] then
		print(Possibilities[1]) -- only goes untill here
		for i, player in players do
			local Explosion = Instance.new("Explosion")
			Explosion.Parent = player.Character
			Explosion.Position = player.Character.HumanoidRootPart.Position
		end
	elseif Possib == Possibilities[2] then
		print(Possibilities[2]) -- only goes untill here
		for i, player in players do
			task.spawn(function()
				player.Character.Humanoid.Health = 0
			end)
		end
	elseif Possib == Possibilities[3] then
		print(Possibilities[3]) -- only goes untill here
		for i, player in players do
			task.spawn(function()
				player.Character.Humanoid.PlatformStand = true
			end)
		end
	end
end)
	
	

Don’t mind me if my code is nested i’m just learning

1 Like

You should remove this variable, because this variable only gets the amount of players when the script is ran. In this case, it’s getting 0 players because the script runs before the player loads. So, for every for i, player in players do, change it to for i, player in playersConnected:GetPlayers() do

Also, you should remove the duplicate Possibilites variable.

local Possibilities = {"explosion", "kill", "platformstand"}

local remote = repStorage:WaitForChild("RemoteEvent")

local Possibilities = {"explosion", "kill", "platformstand"} -- Remove this

Happy to help :grinning:

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.