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
01SS = game.ServerStorage
02plr = game.Players:GetChildren()
03 
04function SM()
05if true then
06    for i = 1, #plr do
07        SS.SWeps.Sword:clone().Parent = plr[i].Backpack
08        print'hi'
09    end
10else
11    print'?'
12    end
13end

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:

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

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

1if true then

is useless.

Try this:

1function SM()
2    for _, player in pairs(game:GetService('Players'):GetPlayers()) do
3        Spawn(function()
4            player:WaitForChild('Backpack')
5            SS.SWeps.Sword:clone().Parent = player.Backpack
6end) 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