Reputation: 3
I am doing a script and output says this " Workspace.Script:30: attempt to index nil with 'Connect' ". The error is on line 30 that has this code:
players.PlayerAdded:Connect(function(player) -- line 30
if player.UserId == alertaCorUserId then
alertaCorEvent:FireClient(player, true)
else
alertaCorEvent:FireClient(player, false)
end
end)
I tried a lot of things, but nothing are working. All help is welcome. I will put here the whole script
local players = game:GetService("Players"):GetPlayers()
if #players > 0 then
local alertaCorUserId = players[math.random(1, #players)].UserId
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local alertaCorEvent = ReplicatedStorage:FindFirstChild("AlertaCorEvent")
if not alertaCorEvent then
alertaCorEvent = Instance.new("RemoteEvent")
alertaCorEvent.Name = "AlertaCorEvent"
alertaCorEvent.Parent = ReplicatedStorage
end
if alertaCorEvent then
players.PlayerAdded:Connect(function(player)
if player.UserId == alertaCorUserId then
alertaCorEvent:FireClient(player, true)
else
alertaCorEvent:FireClient(player, false)
end
end)
alertaCorEvent.OnServerEvent:Connect(function(player, isAlertaCor)
if isAlertaCor then
print(player.Name .. " is Alerta Cor!")
else
print(player.Name .. " isn't Alerta Cor!")
end
end)
else
warn("Erro: alertaCorEvent is nil.")
end
else
warn("No players to be selected")
end
Upvotes: 0
Views: 258
Reputation: 1539
game:GetService("Players")
is the player service, game:GetService("Players"):GetPlayers()
is a table with all connected players. You want to connect an event to the service, not to that table.
Upvotes: 0