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

Why doesn't this script work?

game.Players.PlayerAdded:connect(function(player)
	if player.name == "tkdrocks2017" then
		print("GotThru")
		game.Lighting.Test:Clone().Parent = player.Backpack
	end
end)

My name is tkdrocks2017 and the name of the gear I am cloning is called Test, as stated in lines 2 and 4.

The purpose is to clone a tool from lighting and place it in the player's backpack.

Also, should I make this a localscript or a script?

1 comment

0

Is your script not working, or are you just wondering whether it should be a LocalScript or a Script? To answer that, it should be a server-side script.

3 answers

I don't suggest using Lighting as a storage anymore, now that we have ReplicatedStorage. Also, you should put this in a server-side script.

Edit: Try capitalizing .Name

3 comments

0

What would that do and how would I fix the script?

0

Tell us the problem then? Output? Syntax Errors? This is merely a suggestion.

0

I'm not sure what the problem is. Sorry I can't help with that part.

Don't you need to fix the part where it says TEST and make it so it knows it's a new instance? This is the game pass one by ROBLOX, Maybe it would be possible to edit that and use it instead? Just a suggestion

--------------------
--| WaitForChild |--
--------------------

-- Waits for parent.child to exist, then returns it
local function WaitForChild(parent, childName)
	assert(parent, "ERROR: WaitForChild: parent is nil")
	while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end
	return parent[childName]
end

-----------------
--| Variables |--
-----------------

local GamePassService = Game:GetService('GamePassService')
local PlayersService = Game:GetService('Players')
local InsertService = Game:GetService('InsertService')
local LightingService = Game:GetService('Lighting') --TODO: Use new data store service once that exists

local GamePassIdObject = WaitForChild(script, 'GamePassId')
local ToolAssetsToLoad = WaitForChild(script, 'ToolAssetsToLoad')

local AdminTools = LightingService:FindFirstChild('AdminTools')

-----------------
--| Functions |--
-----------------

-- Makes copies of all the admin tools and puts them in target
local function CloneAdminTools(target)
	for _, tool in pairs(AdminTools:GetChildren()) do
		local toolClone = tool:Clone()
		toolClone.Parent = target
	end
end

-- When a player with the game pass joins, give them the admin tools
local function OnPlayerAdded(player)
	if GamePassService:PlayerHasPass(player, GamePassIdObject.Value) then
		local starterGear = WaitForChild(player, 'StarterGear')
		CloneAdminTools(starterGear)
		if player.Character then -- They've already loaded and won't get their StarterGear until next spawn
			local backpack = WaitForChild(player, 'Backpack')
			CloneAdminTools(backpack)
		end
	end
end

--------------------
--| Script Logic |--
--------------------

-- Create AdminTools if it doesn't exist
if not AdminTools then
	AdminTools = Instance.new('Model')
	AdminTools.Name = 'AdminTools'

	-- Load all of the assets in ToolAssetsToLoad and put them in AdminTools
	for _, intObject in pairs(ToolAssetsToLoad:GetChildren()) do
		if intObject and intObject:IsA('IntValue') and intObject.Value then
			local assetModel = InsertService:LoadAsset(intObject.Value)
			if assetModel then
				local asset = assetModel:GetChildren()[1]
				if asset then
					asset.Parent = AdminTools
				end
			end
		end
	end

	AdminTools.Parent = LightingService
end

PlayersService.PlayerAdded:connect(OnPlayerAdded)

Capitalize ".name" so it is ".Name"

game.Players.PlayerAdded:connect(function(player)
    if player.Name == "tkdrocks2017" then
        print("GotThru")
        game.Lighting.Test:Clone().Parent = player.Backpack
    end
end)

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