Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.