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

Why does this not work?

local Players = game:GetService("Players")
 
function onPlayerAdded(p)

if p.Name == "PLAYER_NAME" then
local Item = game.ServerStorage.ITEMNAME:clone()

        Item.Parent = p.Backpack
Players.PlayerAdded:connect(onPlayerAdded)
end
end


This should give PLAYER_NAME the ITEMNAME on login, however it doesn't. Yes the ITEMNAME is in the ServerStorage.

2 answers

on line 9: game.Players.PlayerAdded:connect(onPlayerAdded)

also idk if this is a problem but i think its Clone() and not clone()

you code has two issues: 1. The event is placed in the function and the function is only called by the event, so the event wont run, as you can see below. 2. functions must be called like this: myFunction(), and the onPlayerAdded() function is not called properly.

local Players = game:GetService("Players")

function onPlayerAdded(p)

if p.Name == "PLAYER_NAME" then
local Item = game.ServerStorage.ITEMNAME:clone()

        Item.Parent = p.Backpack
Players.PlayerAdded:connect(onPlayerAdded) -- the event even if it occurs will never call the function because it's placed in the function and the function is not called properly as well
end
end

The following code will function properly:

local Players = game:GetService("Players")

function onPlayerAdded(p)

if p.Name == "PLAYER_NAME" then
local Item = game.ServerStorage.ITEMNAME:clone()

        Item.Parent = p.Backpack
end
end
Players.PlayerAdded:connect(onPlayerAdded(p))

or you could do this:

local Players = game:GetService("Players")

Players.PlayerAdded:connect(function(p)

if p.Name == "PLAYER_NAME" then
local Item = game.ServerStorage.ITEMNAME:clone()

        Item.Parent = p.Backpack
end
end)

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