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

Attempt to index nil with character??

Asked by 3 years ago

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.

0
Is it a script or localscript NathanBlox_Studios 212 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

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!

Ad

Answer this question