Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does this not work?

Asked by 8 years ago
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

Log in to vote
0
Answered by
Codebot 85
8 years ago

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

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

Ad
Log in to vote
0
Answered by 8 years ago

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)

Answer this question