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()
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)
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.
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 . Thanks for helping, I really appreciate it!