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

[Solved]Why is the sword not being given to the player?

Asked by 10 years ago
SS = game.ServerStorage
plr = game.Players:GetChildren()

function SM()
if true then
    for i = 1, #plr do
        SS.SWeps.Sword:clone().Parent = plr[i].Backpack
        print'hi'
    end
else
    print'?'
    end
end

When I run the function the sword doesn't get given to the player?

SWepsis a model where i put all the weapons and Sword is the sword.

2 answers

Log in to vote
2
Answered by 10 years ago

In line 5, you're not defining what true has to be. You're basically saying if true = true.

A more efficient, and better working code would be:

for i, player in ipairs(game.Players:GetChildren()) do
SS.SWeps.Sword:clone().Parent = player.Backpack
end

Also, since the script is accessing server storage, make sure this isn't a local script.

Ad
Log in to vote
2
Answered by
DeadToMe 105
10 years ago

As what teamkilled said, using

if true then

is useless.

Try this:

function SM()
    for _, player in pairs(game:GetService('Players'):GetPlayers()) do
        Spawn(function()
            player:WaitForChild('Backpack')
            SS.SWeps.Sword:clone().Parent = player.Backpack
end) end end

The breakdown of this:

  1. The for loop iterates through the players.

  2. The Spawn function runs whatever is in the parenthesis (a function) in another thread, thereby allowing the script to continue while another thread is dealing with the individual player.

  3. The new thread then yields until player.Backpack is found.

  4. The sword is cloned into the backpack.

Please remember to upvote if this helps. It helps me.

Answer this question