Tool wont replicate when trying to choose a random player

Hello, I have a “Pass the bomb” game I’m making and I’m struggling to choose a random player to give a tool.
I have tried to search for solutions, but none fit my category. Which is why I’m asking here.
Sorry if the code looks crappy, I’m a bit of an intermediate
This is also my first Dev Forum post.
Anyways, I am trying to make a script where a remote event will replicate the cloned tool onto the server, but its just not firing. Heres the code.

If I don’t use a remote event, it works but it will not replicate (which is not what I want)

Server Code:

--ServerScript (located in ServerScriptService)
local bomb = game.ServerStorage.Bomb
local plrs = game.Players
local event = game.ReplicatedStorage:WaitForChild("Random")
event.OnServerEvent:Connect(function()
while wait(20) do
	local random = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
	bomb:Clone().Parent = random.Backpack
	end
end)

Local Code:

--LocalScript (located in StarterCharacterScripts
local event = game.ReplicatedStorage.Random
event:FireServer()

Any help is appreciated. Thanks

Here is an optimized version of the server script, hopefully fixes the issue with replicating:

local bomb = game.ServerStorage.Bomb
local plrs = game.Players
local event = game.ReplicatedStorage:WaitForChild("Random")

function assignBombToRandomPlayer()
    local playersList = plrs:GetPlayers()
    if #playersList > 0 then
        local randomPlayer = playersList[math.random(1, #playersList)]
        if randomPlayer and randomPlayer:FindFirstChild("Backpack") then
            local clonedBomb = bomb:Clone()
            clonedBomb.Parent = randomPlayer.Backpack
        end
    end
end

event.OnServerEvent:Connect(function()
    while wait(20) do
        assignBombToRandomPlayer()
    end
end)
1 Like

Thanks for helping but, it still doesn’t replicate. I’m pretty sure its an issue on my end. Still, I’ll use this code. I’ll see what I can do about the issue later.

1 Like

It was indeed an issue on my end. I had a script that was breaking all of it. It worked and replicated it after I disabled it. Such an obvious mistake :sweat_smile:. Thanks for helping, I really appreciate it!
image

2 Likes

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