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

Nil Error Issue; Why is the Parameter Indexed as "nil" and How to Fix it?

[Code]

local Players = game:GetService("Players");
local Teams = game:GetService("Teams");

local function onPlayerNew(person) 
		local f3xClone = game.StarterPack.Gold.F3X:Clone();
		f3xClone.Parent = person:WaitForChild("Backpack"); -- Trouble
end
     
local function onPlayerAdded(player)
    	-- Check if team is Gold Builder + if spawned
	if player.Character and player.TeamColor == "Gold" then
		onPlayerNew(player);
	end

	-- Listen for the player (re)spawning 
	player.CharacterAdded:Connect(onPlayerNew());
end
     
-- Iterate over each player already connected
-- to the game using a generic for-loop
for i, player in pairs(Players:GetPlayers()) do
	onPlayerAdded(player);
end
-- Listen for newly connected players
Players.PlayerAdded:Connect(onPlayerAdded);

[Error/Issue]
While running a "Playing Solo" test: ServerScriptService.Script:6: attempt to index local 'person' (a nil value)

[Info]
I have a F3X tool in a folder in StarterPack. I have a script in ServerScriptService. I want only players that are part of a certain team (the TeamColor is "Gold") to have this tool. I have spent a number of hours on the official documentation, but still haven't been able to find out what the issue is. Help would be greatly appreciated.

1 answer

dont add "person" next time use local person = game.Players.LocalPlayer

local Players = game:GetService("Players");
local Teams = game:GetService("Teams");

local function onPlayerNew() 
local person = game.Players.LocalPlayer
        local f3xClone = game.StarterPack.Gold.F3X:Clone();
        f3xClone.Parent = person:WaitForChild("Backpack"); -- Trouble
end

local function onPlayerAdded()
        -- Check if team is Gold Builder + if spawned
    if player.Character and player.TeamColor == "Gold" then
        onPlayerNew(player);
    end

    -- Listen for the player (re)spawning 
    player.CharacterAdded:Connect(onPlayerNew());
end

-- Iterate over each player already connected
-- to the game using a generic for-loop
for i, player in pairs(Players:GetPlayers()) do
    onPlayerAdded(player);
end
-- Listen for newly connected players
Players.PlayerAdded:Connect(onPlayerAdded);

2 comments

0

The thing is that the new function won't be able to take any parameters if I do this

0

P.S. I am not the one who downvoted you

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