So basically I have this stretch of code here:
Saber = script.Parent.part function onKeyPress(inputObject, gameProcessedEvent) if inputObject.KeyCode == Enum.KeyCode.Q then if Saber.ParticleEmitter1.Enabled == true then Saber.ParticleEmitter1.Enabled = false Saber.ParticleEmitter2.Enabled = false Saber.ParticleEmitter3.Enabled = false Saber.ParticleEmitter4.Enabled = false Saber.SurfaceLight.Enabled = false Saber.Off:Play() else Saber.ParticleEmitter1.Enabled = true Saber.ParticleEmitter2.Enabled = true Saber.ParticleEmitter3.Enabled = true Saber.ParticleEmitter4.Enabled = true Saber.SurfaceLight.Enabled = true Saber.On:Play() end end end game:GetService("UserInputService").InputBegan:connect(onKeyPress) function Off () Saber.Off:Play() end script.Parent.Unequipped:connect(Off)
Its a particle emitting light saber, and what its supposed to do is play an on/off sound whenever Q is hit, as Q is the on/off switched assigned, and turn the particles off. The code is almost perfect, however it still plays an on/off when you press Q even when the tool is unequipped/in your backpack. So basically I'm having trouble figuring out how to play the on/off sound when Q is hit ONLY if the tool is equipped.
If any one would be willing to help give me some advice or on what is wrong with the code, then I'll be happy to get that kind of response.
So you're saying the problem is the script is still listening even when it is equipped?
Add a conditional so the script won't fire unless it is equipped.
This can be achieved with the following:
if Saber.Parent.Parent == workspace and inputObject.KeyCode == Enum.KeyCode.Q then
Here's the modified code, except I made a table a for loop can run through.
Saber = script.Parent.part local toggledProps = {Saber.ParticleEmitter1, Saber.ParticleEmitter2, Saber.ParticleEmitter3, Saber.ParticleEmitter4, Saber.SurfaceLight} function onKeyPress(inputObject, gameProcessedEvent) if Saber.Parent.Parent == workspace and inputObject.KeyCode == Enum.KeyCode.Q then if Saber.ParticleEmitter1.Enabled == true then for a = 1, #toggledProps do toggledProps[a].Enabled = false end Saber.Off:Play() else for a = 1, #toggledProps do toggledProps[a].Enabled = true end Saber.On:Play() end end end game:GetService("UserInputService").InputBegan:connect(onKeyPress) script.Parent.Unequipped:connect(function() Saber.Off:Play() end)