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

Why isn't this localscript working? [SOLVED]

Asked by 10 years ago
--TetherBLOX

local player = game.Players.LocalPlayer
local character = player.Character
local mouse = player:GetMouse()
local brick = Instance.new('Part', character)
local weld = Instance.new('Weld', brick)
local light = Instance.new('SpotLight', brick)
local enabled = true

brick.CFrame = CFrame.new(character.Torso.Position)
weld.Part0 = character.Torso
weld.Part1 = brick
brick.Transparency = 1
light.Angle = 180
light.Brightness = 40

mouse.KeyDown:connect(function(key)
    if key == 'f' and enabled then
        light.Enabled = false
        enabled = false
    elseif key == 'f' and not enabled then
        light.Enabled = true
        enabled = true
    end
end)

Error: 12:06:37.016 - Players.Player3.Backpack.LocalScript:11: attempt to index local 'character' (a nil value) 12:06:37.017 - Stack Begin 12:06:37.017 - Script 'Players.Player3.Backpack.LocalScript', Line 11 12:06:37.018 - Stack End

1 answer

Log in to vote
2
Answered by 10 years ago

Sometimes the script loads before the character, thus causing an Error, I add a repeat until nil type code to prevent that;

repeat wait() until game.Players.LocalPlayer and game.Players.LocalPlayer.Character and game.Players.LocalPlayer.Character:FindFirstChild("Torso") and game.Players.LocalPlayer:GetMouse() --This repeats waiting until the script finds/gets these requirements, until then it'll just sit there waiting before going on

Hope this helped!

0
It worked! :) RedneckBane 100 — 10y
0
No problem. :) I prefer 'repeat until nil' loops when it comes to LocalScripts, as damagex443 said, you could use the 'CharacterAdded' event, but I prefer the 'repeat until nil' overall. :P TheeDeathCaster 2368 — 10y
Ad

Answer this question