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

How to insert and remove items from all players inventories?

Asked by 7 years ago

I am making a game where you should only be able to have certain items at certain times. How do i complete this?

1 answer

Log in to vote
0
Answered by
xuefei123 214 Moderation Voter
7 years ago
Edited 7 years ago

Hi there! If you are wanting to remove every player's items on a game, you would have to use a for loop, which would loop through

game.Players:GetChildren()--This returns a table of ALL the players in the game

Here is an example script for removing all of the players items:

local players = game.Players:GetPlayers()
for i,v in pairs(players) do--the variable "v" is equal to the target player
    v.Backpack:ClearAllChildren()--This removes everything in the targetted players backpack
end

To give everyone an item, you would need to define the object you want to give, and then use a for loop again:

local players = game.Players:GetPlayers()
local object = definethis
for i,v in pairs(players) do
    local newObj = object:Clone()
    newObj.Parent = v.Backpack
end

Hope this helped!

0
^ Wrong. You don't need he extra parenthises after "players". I am talking about in the "pairs()". FiredDusk 1466 — 7y
0
How do i insert an existing item from lighting into a players inventory DanGamingTV_YT 0 — 7y
0
Oh yeah, my bad FiredDusk ;p xuefei123 214 — 7y
Ad

Answer this question