StarterPack service breaking my tool

Hello developers, I’ve been trying to code a tool, a flashlight to be specific, the flashlight is finished and is fully functional as long as it’s inside workspace but whenever I move it’s location inside StarterPack it breaks and it no longer works, I’ve searched the dev forum but couldn’t find anything relating to this issue.

My code:

local Player = game.Players.LocalPlayer

local function FindCharacter()
	local character = Player.Character
	return character
end

local character = FindCharacter()

if not character then return end

local tool = script.Parent
local animation = tool:WaitForChild("TurnOn/Off")
local Humanoid = character:WaitForChild("Humanoid")
local animationTrack = Humanoid:LoadAnimation(animation)

local NeonPart = tool.Neon
local SpotLight = NeonPart.SpotLight

local debounce = false
local debounceTime = 1

tool.Activated:Connect(function()
	if not debounce then
		print("tool activated")
		debounce = true
		animationTrack:Play()
		SpotLight.Enabled = not SpotLight.Enabled
		if SpotLight.Enabled then
			NeonPart.Transparency = 0.5
		else
			NeonPart.Transparency = 1
		end

		wait(debounceTime)
		debounce = true
	end
end)

Also this is my first time making an actual topic so I apologize for any issues.

Have you tried debugging after if not character then return end as you’re returning which means if the character isn’t found the rest of the script won’t be executed, which leads to my other question, could it be possible that the character is not being found? If that’s the case, I recommend using Player.CharacterAdded with a :Wait() attached to it so the script yields until the character is found.

1 Like

After debugging you were right, the character was indeed not found, though I’m still unsure on how I can apply the Player.CharacterAdded with a :Wait(), could you please provide an example on how to do so?

Player.CharacterAdded:Wait() | this will yield the script until the players character is added

1 Like

As far as I remember, something like this should fix your issue:

local character = Player.Character or Player.CharacterAdded:Wait();