Need help with Flashlight On/Off feature

This previous post of mine is related to the issue I’m having; however, I will be going over the specific issue I’m having in this post!

My flashlight has an On/Off feature; however, the Part which has the light is in the Workspace. I’ve tried to add that into my script; however, it does not seem to work when trying to turn the flashlight on & off.

I also would like the game to be multiplayer and having the Part just spawn randomly in the Workspace is not very ideal since if there are, let’s say two players in the game, then two Parts will be within the Workspace which will then cause issues (I believe).

Flashlight

Workspace

Screen Shot 2022-12-01 at 1.09.28 PM

Handler script
game:GetService("RunService").RenderStepped:Connect(function()
	local Player = game:GetService("Players")
	local LPlayer = Player.LocalPlayer
	local PlayerGUI = LPlayer:WaitForChild("PlayerGui")
	local Flashlight = script.Parent
	local GUI = PlayerGUI:FindFirstChild("FlashlightGUI")
	local Settings = require(Flashlight.Settings)
	if Settings.BatteryWaste == true then
		GUI.Enabled = true
	else
		GUI.Enabled = false
	end
	GUI.Percent.Text = tostring(math.floor(Flashlight.Battery.Value + 0.5)).."%"
	local Warning = GUI:FindFirstChild("Warning")
	local Battery = Flashlight:WaitForChild("Battery")
	Battery.Changed:Connect(function()
		if Flashlight.Battery.Value <=20 then
			game.Workspace.Sounds.Beep:Play()
			Warning.Visible = true
		else
			Warning.Visible = false
		end
	end)
end)
Function script
local On = false
local Settings = require(script.Parent.Settings)

function Setup()
	script.Parent.Handle.Light.SpotLight.Angle = Settings.Angle
	script.Parent.Handle.Light.SpotLight.Angle = Settings.Range
	script.Parent.Handle.Light.SpotLight.Angle = Settings.Color
	script.Parent.Handle.Light.SpotLight.Angle = Settings.Brightness
end

Setup()
script.Parent.RemoteEvent.OnServerEvent:Connect(function()
	if On == false then
		On = true
		script.Parent.Handle.SwitchOff.Transparency = 1
		script.Parent.Handle.SwitchOn.Transparency = 0
		script.Parent.Handle.Light.Material =  Enum.Material.Neon
		script.Parent.Handle.Light.SpotLight.Enabled = false --true
		script.Parent.Handle.Light.SpotLight2.Enabled = false --true
		script.Parent.Handle.Sound:Play()
		script.Parent.Parent.Parent.Workspace.FlashPart.Spotlight.Enabled = true 
		
		while (On) do
			wait()
			if Settings.BatteryWaste == true then
				wait()
				script.Parent.Battery.Value = script.Parent.Battery.Value - Settings.BatteryWasteAmount
				script.Parent.Battery.Value = (script.Parent.Battery.Value < 0 and 0 or script.Parent.Battery.Value)
			end
		end
	else 
		On = false
		script.Parent.Handle.SwitchOff.Transparency = 0
		script.Parent.Handle.SwitchOn.Transparency = 1
		script.Parent.Handle.Light.Material =  Enum.Material.Glass
		script.Parent.Handle.Light.SpotLight.Enabled = false
		script.Parent.Handle.Light.SpotLight2.Enabled = false
		script.Parent.Handle.Sound:Play()
		script.Parent.Parent.Parent.Workspace.FlashPart.Spotlight.Enabled = false 
		
	end
end)
script.Parent.Unequipped:Connect(function()
	On = false
	script.Parent.Handle.SwitchOff.Transparency = 0
	script.Parent.Handle.SwitchOn.Transparency = 1
	script.Parent.Handle.Light.Material =  Enum.Material.Glass
	script.Parent.Handle.Light.SpotLight.Enabled = false
	script.Parent.Handle.Light.SpotLight2.Enabled = false
	script.Parent.Parent.Parent.Workspace.FlashPart.Spotlight.Enabled = false 
	
end)
Remote script
local player = game.Players.LocalPlayer

script.Parent.Activated:Connect(function()
	script.Parent.RemoteEvent:FireServer()

end)

script.Parent.Equipped:Connect(function()
	local gui = script.Parent.FlashlightGUI:Clone()
	gui.Parent = player.PlayerGui
	script.Parent.Unequipped:Connect(function()
		gui:Destroy()
	end)
end)

Light Part

Workspace

Screen Shot 2022-12-01 at 1.07.44 PM

Local Script
local camera = game.Workspace.Camera
local setup = game.Players.LocalPlayer.Backpack.Flashlight.Function

local flashlightPart = Instance.new("Part")
flashlightPart.Name = "FlashPart"
flashlightPart.Parent = workspace
flashlightPart.CanCollide = false
flashlightPart.Transparency = 1
flashlightPart.Anchored = true

local spotlight = Instance.new("SpotLight")
spotlight.Parent = flashlightPart
spotlight.Brightness = 5



game:GetService("RunService").Heartbeat:Connect(function(deltatime)
	flashlightPart.CFrame = camera.CFrame + camera.CFrame.LookVector * 10
	
	spotlight:Connect(function(setup)
		
	end)
end)

EDIT:

Summary:

I’m just looking for a way to either…

a. Have the On/Off feature connect to the spotlight in the Workspace part

b. Have the spotlight part within the flashlight (to make things easier) but prevent the issues that were present in my previous post

3 Likes

Alright so what exact script is having the problem, so we can dumb it down and help you find a solution faster!

1 Like

Why the heck would you put the SpotLight Part in the workspace and not inside the flashlight itself?

I have a flashlight with the Spotlight in the flashlight Tool. I weld the flashlight to the player’s arm so that when they move during animations the flashlight and Spotlight swing with the arm.
It’s simple and effective.

1 Like

Understandable; however, that’s the issue I was having originally — that the light would swing with the arm. I wanted the light to move with the player’s camera/mouse.

Is there a way I can just have the player’s arm move instead when holding the flashlight?

Hi there!

There is one output error I didn’t notice before —

Connect is not a valid member of SpotLight “Workspace.FlashPart.SpotLight”

Which I believe is related to the LocalScript in the StarterCharacterScripts?

EDIT:

Nevermind the output error — I’m just looking for a way to either
a. Have the On/Off feature connect to the spotlight in the Workspace part

b. Have the spotlight part within the flashlight (to make things easier) but prevent the issues that were present in my previous post

That’s because you can’t connect to the spotlight itself, as its not an event
is there a certain event you’re trying to trigger with the flashlight?

1 Like

No, I was trying to connect the function in the StarterCharacterScript to the other function in the Flashlight tool. I guess that’s just not the way to go about it… sigh

For your Function code, im not sure why you are using a Bool for this, cant you just do this?

SpotLight.Enabled = not SpotLight.Enabled
1 Like

Perhaps, I have a Settings Script that alters the amount of light produced by the flashlight from the original script. I kept it in cause if this didn’t work, I could just start over.

1 Like

In order to connect a function between two scripts, you can either use remote/bindable functions or remote/bindable events.

Article: Remote Events and Functions | Roblox Creator Documentation

1 Like

Oh, also, unless r15 is crucial to your games animations and stuff, you can change it to r6 and it doesn’t swing.

1 Like

I’m currently reading that page and it’s very confusing — I need 3 scripts in ServerScriptService? One to create the function, one to receive the data, and one to send the data? Also, how am I going to connect it to the localscript within the StarterPlayerCharacter?

Also, I’d prefer r15

You could probably use remote events instead.
So, in one script, you can have the function start when the remote event is fired, like this:

game.ReplicatedStorage.insert_name_here.OnServer/ClientEvent:Connect(function()
-- do stuff here
end)

and in the other

-- insert code here
if blah then -- doesn't have to be an if statement just here for yk placeholder
game.ReplicatedStorage.insert_name_here.FireClient/Server

Sorry if I’m being confusing, there’s probably someone who could say it better

Code to move arm/body
StarterplayerScripts

local RunService = game:GetService("RunService")



local Player = game.Players.LocalPlayer

local PlayerMouse = Player:GetMouse()



local Camera = workspace.CurrentCamera



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

local Head = Character:WaitForChild("Head")

local Neck = Head:WaitForChild("Neck")

local Torso = Character:WaitForChild("UpperTorso")
local Waist = Torso:WaitForChild("Waist")

local Arm = Character:WaitForChild("RightUpperArm")

local Shoulder = Arm:WaitForChild("RightShoulder")



local Humanoid = Character:WaitForChild("Humanoid")

local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")



local NeckOriginC0 = Neck.C0

local ShoulderOriginC0 = Shoulder.C0

local WaistOriginC0 = Waist.C0



Neck.MaxVelocity = 1/2



RunService.RenderStepped:Connect(function()

	local CameraCFrame = Camera.CoordinateFrame



	if Character:FindFirstChild("UpperTorso") and Character:FindFirstChild("Head") then

		local ArmLookVector = Arm.CFrame.lookVector

		local HeadPosition = Head.CFrame.p
		
		local TorsoLookVector = Torso.CFrame.lookVector


		if Neck and Shoulder and Waist then

			if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then

				local Point = PlayerMouse.Hit.p



				local Distance = (Head.CFrame.p - Point).magnitude

				local Difference = Head.CFrame.Y - Point.Y



				Neck.C0 = Neck.C0:lerp(NeckOriginC0 * CFrame.Angles(-(math.asin(Difference / Distance) ), (((HeadPosition - Point).Unit):Cross(ArmLookVector)).Y, 0), .5 / 2)

				Shoulder.C0 = Shoulder.C0:lerp(ShoulderOriginC0 * CFrame.Angles(-(math.asin(Difference / Distance)), (((HeadPosition - Point).Unit):Cross(ArmLookVector)).Y, 0), .5 / 2)
				
				Waist.C0 = Waist.C0:lerp(WaistOriginC0 * CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5, 0), 0.5 / 2)
				
			end

		end

	end	

end)

I inserted a part into the tool w/ a spotlight.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.