Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I edit this script to make the light turn off after another click?

Asked by 5 years ago

I am editing a flashlight script that works with a flashlight I built. When you click, the SpotLight in a part is enabled like a normal flashlight would and turns off when you click again. It is also supposed to make another part called "lighton" visible (basically a yellowish neon part at the front that adds to the visual effect with the SpotLight that the flashlight is turned on), but it isn't working. It changes the transparency of the neon part to 0, but doesn't change it back to 1 when you click again like the SpotLight turns off after you click.

Hopefully that made enough sense, and I apologize for my bad scripting skills. Here's the script:

player = game.Players.LocalPlayer
tool = script.Parent

repeat wait() until player

tool.Equipped:connect(function(mouse)
    mouse.Button1Down:connect(function()
        tool.Light.SpotLight.Enabled = not tool.Light.SpotLight.Enabled
        tool.Handle.Sound:Play()
        tool.lighton.Transparency = not tool.lighton.Transparency
    end)
end)

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

It is setting the Transparency property to false. In Lua, anything but false and nil is truthy. Numbers, strings, and tables are examples of truthy values. Line 10 is basically tool.lighton.Transparency = false. I do not know if you want it to be 0 when the SpotLight is Enabled, or if you want it to be 1. I will assume you want it to be 1, but you can always change the numbers.

 tool.lighton.Transparency = tool.Light.SpotLight.Enabled and 1 or 0
-- if the spotlight is enabled, we will set it to 1, if it's not, set to 0

Final product:

local player = game.Players.LocalPlayer -- ALWAYS use local variables 
local tool = script.Parent

-- the loop was unnecessary 

tool.Equipped:Connect(function(mouse) -- connect is deprecated, use Connect
    mouse.Button1Down:Connect(function()
        tool.Light.SpotLight.Enabled = not tool.Light.SpotLight.Enabled
        tool.Handle.Sound:Play()
        tool.lighton.Transparency = tool.Light.SpotLight.Enabled and 1 or 0
    end)
end)


0
You are a life-saver. Thank you so much! SheriffTaco 13 — 5y
Ad

Answer this question