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

How do I make this so that whenever a person enters, they receive a weapon?

function onPlayerAdded(players)
	for -- I don't know what else to do?
game.Lighting[script.Parent.Name]:Clone().Parent = players[i].Backpack
game.Lighting[script.Parent.Name]:Clone().Parent = players[i].StarterGear

	end
	end

It's not in starter pack because I made a GUI where when you press a textbutton with a weapon's name on it, it will generate that weapon and put it in everyone's backpack and startergear. But I want it so that when other people join the server, they get the weapons as well.

Here's the script that generates a weapon when someone clicks the GUI, for some reason for i,v players do doesn't give the players a weapon, it only gives whoever clicks the GUI a weapon

script.Parent.MouseButton1Click:connect(function()	
local players = game.Players:GetChildren()
for i = 1, #players do
game.Lighting[script.Parent.Name]:Clone().Parent = players[i].Backpack
game.Lighting[script.Parent.Name]:Clone().Parent = players[i].StarterGear
onPlayerAdded()


end
end)

3 comments

0

for i, v in pairs(players) do

0

@JohnTUnderwood it breaks

0

right my bad. look below vvvvvvvvvv

1 answer

First, don't store data in Lighting. Store it in ServerStorage (to make it so only the server can access it) or ReplicatedStorage (both the client and the server can access it).

Second, in onPlayerAdded, provided you connected it to game.Players.PlayerAdded, the variable "players" will only be the player who joined.

Therefore, no for loop is needed.

Instead, run the code you're already running but instead of players[i], use player.

2 comments

0

This is the correct answer.

0

Still doesn't work, it should but it doesn't

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