Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do I remove the random player selected from the table?

Error: ServerScriptService.dataStore:69: bad argument #2 (number expected, got Object) table.remove(playersPlaying, Simon)

local playersPlaying = require(game.ServerScriptService.ModuleScript)


workspace.Part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		table.insert(playersPlaying, hit.Parent)
		wait(2)
		print("Killing players")
		for i, v in ipairs(playersPlaying) do
		print("Choosing random player")
		local Simon = playersPlaying[math.random(1, #playersPlaying)]
		print("Random player is: "..Simon.Name)
		
		for i=0, 4 do
			print(i)
			i = i - 1
			wait(1)
		end
		
		table.remove(playersPlaying, Simon)
		print(v)
	  end
	end
end)
	

1 answer

Selecting a random player from an array is fairly straightforward. You simple have to get an array of players using Players:GetPlayers(), and since table.remove() returns the removed index, you could do something like

local Players = game:GetService("Players")
local plist = Players:GetPlayers()
-- the below will now select (and remove) a random player from the table
local selected = table.remove(plist, math.random(0, #plist))

1 comment

0

Can you add me discord Aft3rL1fe#8610 when we fix the problem, we will post the answer

This discussion is preserved as it was. New answers, comments, and votes are closed.