Help! Flashlight script randomly errors!

I’m trying to make a relatively simple flashlight toggle script with animations, however, it won’t find the “Idle” animation inside of the animation folder.

Client Script:

local Player = game.Players.LocalPlayer
repeat wait() until script.Parent:FindFirstChild("Client_Server")
repeat wait() until script.Parent.Animations:FindFirstChild("Idle")
local Hum = Player.Character.Humanoid

local Remote = script.Parent:FindFirstChild("Client_Server")
local Status = "Off"

local Animations = {
	Idle = Hum:LoadAnimation(script.Parent.Idle),
	TurnOn = Hum:LoadAnimation(script.Parent.On),
	TurnOff = Hum:LoadAnimation(script.Parent.Off),
}

Animations.TurnOn:GetMarkerReachedSignal("Light"):Connect(function()
	script.Parent.Handle.Toggle:Play()
	script.Parent.Client_Server:FireServer("On")
end)
Animations.TurnOff:GetMarkerReachedSignal("Light"):Connect(function()
	script.Parent.Handle.Toggle:Play()
	script.Parent.Client_Server:FireServer("Off")
end)

script.Parent.Activated:Connect(function()
	if Status == "Off" then
		Animations.TurnOn:Play()
		Status = "On"
	elseif Status == "On" then
		Animations.TurnOff:Play()
		Status = "Off"
	end
end)

Server Script:

script.Parent.Client_Server.OnServerEvent:Connect(function(Thing)
	if Thing == "On" then
		script.Parent.Handle.LightRing.Enabled = true
		script.Parent.Handle.Attachment.InnerLight.Enabled = true
	elseif Thing == "Off" then
		script.Parent.Handle.LightRing.Enabled = false
		script.Parent.Handle.Attachment.InnerLight.Enabled = false
	end
end)

Error:
image

I was getting this same error aswell, it doesn’t detect items when inside a tool, its very weird and i never found a fix to it :frowning:

1 Like

Things take a while to load, so the objects inside the tool or folder or whatever it’s in haven’t loaded before the script ran.
Use Instance:WaitForChild

2 Likes

You shouldn’t be using repeat wait() until. Just use the event based alternative. You also did not reference your animation properly.
Switch your client script to this:

--//Services
local Players = game:GetService("Players")

--//Variables
local Player = Players.LocalPlayer
local Hum = Player.Character.Humanoid
local Animations = script.Parent.Animations
local Client_Server = script.Parent:WaitForChild("Client_Server")

--//Controls
local Status = "Off"

--//Tables
local Animations = {
	Idle = Hum:LoadAnimation(Animations.Idle),
	TurnOn = Hum:LoadAnimation(Animations.On),
	TurnOff = Hum:LoadAnimation(Animations.Off),
}

--//Functions
Animations.TurnOn:GetMarkerReachedSignal("Light"):Connect(function()
	script.Parent.Handle.Toggle:Play()
	Client_Server:FireServer("On")
end)

Animations.TurnOff:GetMarkerReachedSignal("Light"):Connect(function()
	script.Parent.Handle.Toggle:Play()
	Client_Server:FireServer("Off")
end)

script.Parent.Activated:Connect(function()
	if Status == "Off" then
		Animations.TurnOn:Play()
		Status = "On"
	elseif Status == "On" then
		Animations.TurnOff:Play()
		Status = "Off"
	end
end)
1 Like

Funny story, i’ve already fixed it by changing the entire script to be a server-script. Sorry for this!!