01 | SS = game.ServerStorage |
02 | plr = game.Players:GetChildren() |
03 |
04 | function SM() |
05 | if true then |
06 | for i = 1 , #plr do |
07 | SS.SWeps.Sword:clone().Parent = plr [ i ] .Backpack |
08 | print 'hi' |
09 | end |
10 | else |
11 | print '?' |
12 | end |
13 | end |
When I run the function the sword
doesn't get given to the player?
SWeps
is a model where i put all the weapons and Sword
is the sword.
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:
1 | for i, player in ipairs (game.Players:GetChildren()) do |
2 | SS.SWeps.Sword:clone().Parent = player.Backpack |
3 | end |
Also, since the script is accessing server storage, make sure this isn't a local script.
As what teamkilled said, using
1 | if true then |
is useless.
Try this:
1 | function 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 |
6 | end ) end end |
The breakdown of this:
The for loop iterates through the players.
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.
The new thread then yields until player.Backpack is found.
The sword is cloned into the backpack.
Please remember to upvote if this helps. It helps me.