What is wrong with this script?

for _, player in pairs(game.Players:GetChildren()) do
   game.ReplicatedStorage.ClassicSword:Clone()
   game.ReplicatedStorage.ClassicSword:Clone()
   game.ReplicatedStorage.ClassicSword.Parent = player.Backpack
   game.ReplicatedStorage.ClassicSword.Parent = player.StarterPack
end

It is in a script in serverscriptservice, and the error is ClassicSword is not a valid member of ReplicatedStorage

The thing that is wrong is that ClassicSword is not a valid member of ReplicatedStorage.

1 Like

It is part of ReplicatedStorage

I assume that at the time the script executes, it is not, that’s why I suggest to do the following:

for _, player in pairs(game.Players:GetChildren()) do
   local sword = game.ReplicatedStorage.ClassicSword:Clone()
   sword.Parent = player.Backpack
   sword = game.ReplicatedStorage.ClassicSword:Clone()
   sword.Parent = player.StarterPack
end

I fixed the problem in your script, becuase you were changing the parent not of the ClassicSword copy but the original one.

1 Like

Use :WaitForChild("ClassicSword") instead of just .ClassicSword the game assets don’t load in right away you know.

Right when this script runs there won’t be any players in the game yet, so when the script loops through all the players and gives all of them swords, there won’t be any players to get them. What you’d have to do is use PlayerAdded to detect whenever anyone joins the game and then give them the sword.

game.Players.PlayerAdded:Connect(function(Player)
    local ClassicSword = game.ReplicatedStorage:WaitForChild( "ClassicSword"):Clone()
    ClassicSword.Parent = Player.Backpack
end)

I’m not really sure what you’re trying to do with that line. StarterPack isn’t a valid member of player, it seems pretty random.

There’s a much easier way to do this though, you can just put ClassicSword in StarterPack, and the game will automatically give it to everyone when they join.

The problem is not that he has to wait for the sword, because it’s in replicated storage when the game starts, the problem was that he was changing the parent of the original sword in replicated storage, not the copied one.

The game’s assets many times load in after the scripts start running. This is most likely the problem, but there is another problem that needs to be fixed.

I also tried this to remove the swords but it says ClassicSword is not a valid member of Backpack

  '''

for _, player in pairs(game.Players:GetChildren()) do
local sword = player.Backpack.ClassicSword
local sword2 = player.StarterGear.ClassicSword
sword.Parent = game.ReplicatedStorage
sword2.Parent = game.ReplicatedStorage
end

                          '''
for _, player in pairs(game.Players:GetChildren()) do
    local sword = player.Backpack:FindFirstChild("ClassicSword")
    local sword2 = player.StarterGear:FindFirstChild("ClassicSword")
    if sword then sword:Destroy() end
    if sword2 then sword2:Destroy() end
end

I added more to my previous reply.

It seem like if you are equipping the sword, it won’t be destroyed

Yes because that’s how Roblox works, an equipeed item is moved to the Player’s character, you will have to look for the sword inside of the character too and remove it from there if it exists.

Oh ok that’s why. What is it called?

Is the sword a child of the character or is it a child of a child of the character?

local char = player.Character
if char then
    local sword = char:FindFirstChild("ClassicSword")
    if sword then sword:Destroy() end
end

It’s parented directly under the character, and then it is moved back to the player’s backpack when it’s unequipped.

read my previous reply!!!