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

Why isn't this localscript working? [SOLVED]

--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

✓ Accepted answer

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!

2 comments

0

It worked! :)

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

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