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

How do I make it so everyone gets an item when an admin loads a command?

Asked by 8 years ago

The Script is pretty basic, I just can't find out what is wrong with it.

local player = game.Players:GetChildren()
admin = {"Player"} 
game.Players.PlayerAdded:connect(function(nP)
for _,v in pairs(admin) do
if nP.Name == v then
nP.Chatted:connect(function(msg)
if msg == "load/gun" then 
local x = game.Lighting.SKP:Clone()
x.Parent = player.Backpack 
-- x.Parent = game.Workspace  
end
end)
end
end
end)

--[[ If I remove line 1 and replace line 9 with line 10, the script works ]]--

2 answers

Log in to vote
0
Answered by
Discern 1007 Moderation Voter
8 years ago

You are attempting to parent the cloned gun into a table (a table of the children of the Players service). You need to loop through the table and give each player it like so:

admin = {"Player"} 
game.Players.PlayerAdded:connect(function(nP)
for _,v in pairs(admin) do
if nP.Name == v then
nP.Chatted:connect(function(msg)
if msg == "load/gun" then 
for i,v in pairs(game.Players:GetChildren()) do
local x = game.Lighting.SKP:Clone()
x.Parent = v.Backpack
end
end
end)
end
end
end)

If I helped you out, be sure to accept my answer! :D

0
Both of your answers work, just giving it to Jason b/c he answered first User#6200 10 — 8y
0
If multiple answers are correct what I do is accept one and upvote the rest to be fair drew1017 330 — 8y
Ad
Log in to vote
1
Answered by
ImageLabel 1541 Moderation Voter
8 years ago

You were mostly on the right path. However, instead of using GetChildren, I would recommend using the GetPlayers function, which returns a table of all player objects, instead of all children... because if you were to have something other than a player in the Players service, it would take a little more to sort them out.

I would also recommend using ReplicatedStorage instead of Lighting... because as revealed in their names, ReplicatedStorage is meant for storage, and Lighting for Lighting.

Using just what you have, I put the following together. I'm using the next iterator function instead of pairs. There's not much of a difference, expect the fact that pairs uses next.

local players = game:GetService('Players'):GetPlayers()
local storage = game:GetService('ReplicatedStorage')
local admins = {'Player'}

players.PlayerAdded:connect(function( player )
    for index, admin in next, admins do
        if player.Name:lower() == admin:lower() then
        -- to avoid case sensitivity issues, all lower case
        print('isAdmin', player)

            player.Chatted:connect(function( message )
                if message:lower() == 'load/gun' then
                -- again, avoiding case-sensitivity issues
                local gun = storage:FindFirstChild('SKP')
                -- find "SKP" in `storage`, defined above
                    if gun then
                        for index, player in next, players do
                        local clone = gun:Clone()
                        clone.Parent = player.Backpack
                        end
                    end
                end
            end)
        end
    end
end)

Answer this question