Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I make this give all players a random sword?

Asked by 9 years ago

I'm trying to finish my game. However, there is an issue that occurs. The script gives only ONE player a sword, instead of everyone in the server.

Swords = game.ReplicatedStorage.Swords:GetChildren()
--Select a random sword
selected = Swords[math.random(1,#Swords)]
selected:Clone().Parent = game.Players.LocalPlayer.Backpack

1 answer

Log in to vote
1
Answered by 9 years ago
for i,v in pairs (game.Players:GetPlayers()) do
Swords = game.ReplicatedStorage.Swords:GetChildren()
--Select a random sword
selected = Swords[math.random(1,#Swords)]
selected:Clone().Parent = v.Backpack
end

I used a pairs loop to get all the players in Players and ran the function to each one of them. This should only be ran once though, so it'd be best in a server script. (not a localscript)

Ad

Answer this question