How would I do this?

So how would I make a script where a random player is chosen and given a tool. If that player dies, the tool will be given randomly to someone else
function changepotato()

local players = game:GetService(“Players”)

local randomplayer = players[math.random(1, #players)]

local potato = game:GetService(“ServerStorage”)[“Pass Potato”]

local potatoclone = potato:Clone()

potatoclone.Parent = randomplayer.Backpack

end

I don’t think this code will work

2 Likes

I’d get a random player with something like this:

	local AllPlayers = game:GetService("Players"):GetPlayers()

	local RandomPlayer = math.random(1, #AllPlayers)
	print(AllPlayers[RandomPlayer])

Assuming you are attempting to make a rendition of hot potato, you can handle deaths yourself (ie. when the potato explodes etc, kill the player holding it & assign a new player to hold it)

Try this out:

local PS = game:GetService("Players")
local SS = game:GetService("ServerStorage")

local function ChangePotato()
	local randomPlayer = PS:GetPlayers()[math.random(1, #PS:GetPlayers())]
	
	local potatoClone = SS["Pass Potato"]:Clone()
	potatoClone.Parent = randomPlayer.Backpack
	
	randomPlayer.Died:Connect(ChangePotato)
end

ChangePotato()