Why this script is not working

Hello I’m making a script to pick a random player and attach an accessory to the chosen player.

Script:

local Players = game:GetService("Players"):GetPlayers()
local randomPlayer
game.ReplicatedStorage.StartGame.Event:Connect(function()
if #Players > 0 then
randomPlayer = Players[math.random(#Players)]
local character = randomPlayer.Character
character.Humanoid:AddAccessory(game.ReplicatedStorage.BombGame:Clone())
game.ReplicatedStorage.BombStartGui:FireClient(randomPlayer)
end
end)

You’re setting Players as all the players when the script first runs, so you will be getting an empty list. You should be creating this variable inside of your StartGame event to get the current list of players.

2 Likes