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

Attempt to index nil with character??

My script :

local Player = game:GetService("Players").LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:FindFirstChildOfClass("Humanoid")
game.ReplicatedStorage.ToolGive.OnServerEvent:Connect(function(player) --checks for event
	local tool = game.Lighting.DarkSword:Clone() --gets tool
	tool.Parent = game.Players.LocalPlayer:FindFirstChild("Backpack") --gives tool
end)		--ends lol

: This is supposed to give you a weapon, the first three lines i added because the backpack wouldnt load in and apparently you need the give the character time to load in.

I keep getting either 3 of the following errors:

Attempt to index nil with character Attempt to index nil with backpack Attempt to index nil with FindFirstChild

I have no idea what to do at this point, ive been trying to fight this bug for like 2 hours. Any insight would be appreciated.

1 comment

0

Is it a script or localscript

1 answer

✓ Accepted answer

So, from Line 4, it looks like it is Server Script. But LocalPlayer is not supported for Server/Normal scripts. LocalPlayer is only for LocalScripts.

Also, if you are confused on how you can get the player. Well, its simple, you are receiving remote event fired from Client Side, so the player parameter you have in the function, that is your player.

So fix that :

game.ReplicatedStorage.ToolGive.OnServerEvent:Connect(function(player) 
    local tool = game.Lighting.DarkSword:Clone()
    tool.Parent = player:WaitForChild("Backpack") -- Gets the backpack of the player, who fired the remote event
end)

Lemme know if it helps!

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