Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Light Script not working as intended?

So i have this flash light script its inside the gun inside Replicated Storage and then i have a pick up system where it clones it from Replicated Storage The light should ONLY activate when i press l when my gun is equipped and disappear if i unequip , right now i can press l and it will activate just by staying in my inventory(not equipped) and it doesent disappear if i unequip the gun.

	local tool = game.ReplicatedStorage.M4A41
	local plr = game.Players.LocalPlayer
	local mouse = game.Players.LocalPlayer:GetMouse()
	function Light()
	    player = game.Players.LocalPlayer
	    playerChar = player.Character
	    playerLight = playerChar.Torso:FindFirstChild("Light")
	    if playerLight then
	        playerLight:Destroy()
	    else
	        light = Instance.new("SurfaceLight",playerChar:FindFirstChild("Torso"))
	        light.Name = "Light"
	        light.Range = 15
	        light.Brightness = 5
	        light.Shadows = true
	        light.Enabled = true --Its something like this
	 
	 
	        local play = Instance.new("Sound",playerChar:FindFirstChild("Head"))
	        play.SoundId = "http://www.roblox.com/asset/?id=198914875"
	        play:Play()
	 
	    end
	end
	mouse.KeyDown:connect(function(key)
	key = key:lower()
	if key == "l" and tool.Equipped then
	    Light()
	tool.Unequipped:Connect(function()
	playerChar.Torso.Light:Destroy()
	end)
	end
	end)

1 answer

if you want it to only activate when equipped, you can easily set a bool value for when the tool is equipped or not. Also, don't use the parent parameter of Instance.new(). It's old, slower, and deprecated. Also, I recommend UserInputService instead of KeyDown. If this script, it may be because the tool is in ReplicatedStorage and not actually in the player's backpack, so just be aware of that.

local tool = game.ReplicatedStorage.M4A41
local plr = game.Players.LocalPlayer
local mouse = game.Players.LocalPlayer:GetMouse()
local equipped = false

function Light()
    player = game.Players.LocalPlayer
    playerChar = player.Character
    playerLight = playerChar.Torso:FindFirstChild("Light")
    if playerLight then
        playerLight:Destroy()
    else
        light = Instance.new("SurfaceLight")
        light.Name = "Light"
        light.Range = 15
        light.Brightness = 5
        light.Shadows = true
        light.Enabled = true --Its something like this
	light.Parent = playerChar:WaitForChild("Torso") or playerChar:WaitForChild("HumanoidRootPart")
        local play = Instance.new("Sound")
        play.SoundId = "http://www.roblox.com/asset/?id=198914875"
	play.Name = "LightSound"
	play.Parent = playerChar:WaitForChild("Head")
        play:Play()
    end
end

tool.Unequipped:Connect(function()
	equipped = false
	if playerChar:WaitForChild("Torso"):FindFirstChild("Light") then
		playerChar:WaitForChild("Torso"):FindFirstChild("Light"):Destroy()
	elseif playerChar:WaitForChild("HumanoidRootPart"):FindFirstChild("Light") then
		playerChar:WaitForChild("HumanoidRootPart"):FindFirstChild("Light"):Destroy()
	end
	if playerChar:WaitForChild("Head"):FindFirstChild("LightSound") then
		playerChar:WaitForChild("Head"):FindFirstChild("LightSound"):Destroy()
	end
end)

tool.Equipped:Connect(function()
	equipped = true
end)

game:GetService("UserInputService").InputBegan:Connect(function(input,gameProcessed)
	if gameProcessed or equipped == false then return end
	if input.KeyCode == Enum.KeyCode.L then
		Light()
	end
end)

3 comments

0

I tried the code and its not working. Its a local script inside the gun inside Replicated Storage should it be somewhere else?

0

I've tried putting it in ServerScriptService, Workspace , StarterCharScripts , StarterPlayerScript and it doesent seem to work any idea why could there something be wrong with the code?

0

the gun is a tool, and it should be in the player's backpack. the script should go inside the gun with the tool variable as script.Parent

This discussion is preserved as it was. New answers, comments, and votes are closed.