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

attempt to index nil with waitforchild()?

Asked by 4 years ago

I am working on a singleplayer game and I want to create a flashlight, but the script I am using doesn't work.

Error: ServerScriptService.Other.FlashlightSetup:5: attempt to index nil with 'WaitForChild'

local players = game:GetService("Players")

local function createFlashlight()
    local character = game.Workspace:FindFirstChildWhichIsA("Model")
    local torso = character:WaitForChild("Torso") -- attempt to index nil with 'WaitForChild'

    local light = Instance.new("PointLight", torso)
    light.Name "flashlight"
    light.Color = "White"
    light.Enabled = false
    light.Brightness = 30
end

players.PlayerAdded:Connect(createFlashlight)

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The player is not your character. They are two separate things. When your player is added the character is not yet loaded.

I'd advise in this case to remove the PlayerAdded part and move this script to StarterCharacterScripts inside StarterPlayer. This means the script will automatically be put inside every character that spawns in.

The only thing you'd have to change at that point is local character = script.Parent

Ad

Answer this question