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 ]]--
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
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)