How to disable Torso Instance.new('SpotLight') after you stop touching a part

Hello,

So I got it to work to where when you enter the part, a flashlight GUI and a SpotLight attached to the Torso appear. I’m struggling a bit though to deactivate the SpotLight after exiting and I’m not so sure how to go about it.

Footage:


Should be obvious what I’m going for.

How could I make the SpotLight disappear along with the GUI?

Setup:

The part being in Workspace
image

The code inside “Script”

local zone = script.Parent
local FlashlightOn = game.Workspace.FlashlightOn
local FlashlightOff = game.Workspace.FlashlightOff
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local FlashlightOn = ReplicatedStorage.FlashlightOn
local FlashlightOff = ReplicatedStorage.FlashlightOff


zone.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
		if plr then
			if not plr.PlayerGui:FindFirstChild("GUIClonedfromtouchblock") then
				local clonedgui = script.Parent:FindFirstChildOfClass("ScreenGui"):Clone()
				FlashlightOn:FireClient(plr)
				clonedgui.Name = "GUIClonedfromtouchblock"
				clonedgui.Parent = plr.PlayerGui
				script.Parent.TouchEnded:Connect(function(hit2)
					if hit == hit2 then
						FlashlightOff:FireClient(plr)
						game.Debris:AddItem(clonedgui,0)
					end
				end)
			end
		end
	end
end)

The 2 RemoteEvents in ReplicatedStorage
image

The 2 LocalScripts inside StarterGui
image

The code inside of FlashlightOff

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Remote = ReplicatedStorage:WaitForChild("FlashlightOff")

local FlashlightOff = workspace.FlashlightOff

Remote.OnClientEvent:Connect(function()
	FlashlightOff:Play()
end)

The code inside of FlashlightOn

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Remote = ReplicatedStorage:WaitForChild("FlashlightOn")

local FlashlightOn = workspace.FlashlightOn

Remote.OnClientEvent:Connect(function()
	FlashlightOn:Play()
	light = Instance.new('SpotLight')
	light.Brightness = 5
	light.Parent = game.Players.LocalPlayer.Character.Torso
end)

I’m not so sure how I’d disable

Instance.new('SpotLight')

with the LocalScript FlashlightOff to make it disappear along with the GUI. I’d appreciate some help!!

could you send the model for it, so i can test it

Sure, sent it to you in your dms.

1 Like

so you forgot to destroy the spotlight in the players torso, hope this helps

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Remote = ReplicatedStorage:WaitForChild("FlashlightOff")

local FlashlightOff = workspace.FlashlightOff

Remote.OnClientEvent:Connect(function()
game.Players.LocalPlayer.Character.Torso:WaitForChild("SpotLight"):Destroy()
	FlashlightOff:Play()
end)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Torso = Character:WaitForChild("Torso")

local Remote = ReplicatedStorage:WaitForChild("FlashlightOff")

local FlashlightOff = workspace.FlashlightOff

Remote.OnClientEvent:Connect(function()
	FlashlightOff:Play()
	task.wait(0.5)
	local Light = Torso:FindFirstChild("SpotLight")
	if Light then
		Light:Destroy()
	end
end)

Just a slight fix to what you already provided.

1 Like

Thanks for the help, it worked at first with deactivating the light but it kinda permanently disappeared after 2-3 times of going in and out.

1 Like