Help With a Flashlight Toggle Button

Hello everyone, I made a cool GPS GUI, and on the side, it has a ‘light’ function which would activate a backlight for the device IRL. In my game, I was hoping to get it so that when the button is clicked (I have a button alread) a light is attached to the player’s torso. Clicking the button again will toggle the light off/on)

image

Any help is greatly appreciated.

Add a Spotlight to the player’s torso, then you can Enable true or false to turn it on or off.

1 Like
local player = game.Players.LocalPlayer
local char = player.Character
local light = char.Torso.WaitForChild("SpotLight")

script.Parent.MouseButton1Click:Connect(function()
	light.Enabled = false
	
end)

How do I look for both Torso and SpotLight in the same line?

local player = game.Players.LocalPlayer
local char = player.Character
local light = char.Torso:WaitForChild("SpotLight")

script.Parent.MouseButton1Click:Connect(function()
	light.Enabled = not light.Enabled
end)

That should work

  14:11:12.550  Torso is not a valid member of Model "Workspace.MicrosoftDeveloper1"  -  Client - LocalScript:3
  14:11:12.550  Stack Begin  -  Studio
  14:11:12.550  Script 'Players.MicrosoftDeveloper1.PlayerGui.GPS.GPSFrame.LightFunction.LocalScript', Line 3  -  Studio - LocalScript:3
  14:11:12.550  Stack End  -  Studio

Getting this error. I got it before as well.

Change Torso to whatever you have parented the light to. I assume it’s HumanoidRootPart or UpperTorso

image
I think it is parented to SpotLight. I changed it to HumanoidRootPart and edited the script to match, but get the same error.

HumanoidRootPart is not a valid member of Model "Workspace.MicrosoftDeveloper1"  -  Client - LocalScript:3

Most likely, the problem is that you are trying to reference the HumanoidRootPart before it even exists. You see, Roblox characters take a while to fully load in, which is why you should always use the :WaitForChild() function while defining or referencing a body part.

Here is an updated script:

local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local spotlight = character.HumanoidRootPart:WaitForChild("SpotLight")

script.Parent.MouseButton1Click:Connect(function()
		spotlight.Enabled = not spotlight.Enabled
end)

Please tell me if you encounter any bugs or errors with this script!

1 Like
  15:09:26.037  HumanoidRootPart is not a valid member of Model "Workspace.MicrosoftDeveloper1"  -  Client - LocalScript:3
  15:09:26.037  Stack Begin  -  Studio
  15:09:26.037  Script 'Players.MicrosoftDeveloper1.PlayerGui.GPS.GPSFrame.LightFunction.LocalScript', Line 3  -  Studio - LocalScript:3
  15:09:26.037  Stack End  -  Studio

It keeps happening… I really don’t know how to fix this. I’m using an R6 game, the code is in a LocalScript underneath a textButton.

local players= game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart= character:WaitForChild("HumanoidRootPart")
local spotLight = humanoidRootPart:WaitForChild("SpotLight")

script.Parent.MouseButton1Click:Connect(function()
	spotLight.Enabled = not spotLight.Enabled
end)

Is it a local script, right?
Also, if you want this flashlight to be shown to everyone in game, you should either use a Script or a RemoteEvent [with a local script and a script on server].

3 Likes