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

How do I have a command run on unequipping?

Asked by 5 years ago

I'm making a flashlight and I want it to turn off when unequipped, but the script won't register when the item is unequipped.

(Script)

Tool = script.Parent
InUse = false
FlashlightMesh = Tool.FlashlightMesh
SpotLight = FlashlightMesh.SpotLight
IdleAnim = script.Idle
RepStorage = game.ReplicatedStorage
Event = RepStorage.Items.FlashLightEvent

Tool.Activated:connect(function()
    print(Tool.Name.. " Activated")
    LightingControl = Tool.Parent:WaitForChild('LightingControl')
    if InUse == false then
        InUse = true
        SpotLight.Enabled = true
        Event:FireAllClients(20,70,false)
        print("1A:")
    elseif InUse == true then
        InUse = false
        SpotLight.Enabled = false
        Event:FireAllClients(20,70,true)
        print("2A:")
    end
end)

Tool.Equipped:connect(function()
    local Humanoid = Tool.Parent:WaitForChild('Humanoid')
    IdleAnimLoaded = Humanoid:LoadAnimation(IdleAnim)
    equipped = true
    IdleAnimLoaded:Play()
end)

Tool.Unequipped:connect(function()
    print("Unequiped")
    equipped = false
    IdleAnimLoaded:Stop()
    LightingControl = Tool.Parent:WaitForChild('LightingControl')
    if InUse == true then
        SpotLight.Enabled = false
        Event:FireAllClients(20,70,true)
        InUse = false
        print("3A:")
    end
end)

(Local Script)

RepStorage = game.ReplicatedStorage
Event = RepStorage.Items.FlashLightEvent
Tool = script.Parent

Event.OnClientEvent:Connect(function(Ambience,FogEnding,On)
    if On == false then
        local Character = Tool.Parent
        local LightingControl = Character:WaitForChild('LightingControl')
        LightingControl.Ambient.Value = LightingControl.Ambient.Value + Ambience
        LightingControl.FogEnd.Value = LightingControl.FogEnd.Value + FogEnding
        print("1B:")
    elseif On == true then
        local Character = Tool.Parent
        local LightingControl = Character:WaitForChild('LightingControl')
        LightingControl.Ambient.Value = LightingControl.Ambient.Value - Ambience
        LightingControl.FogEnd.Value = LightingControl.FogEnd.Value - FogEnding
        print("2B:")
    end
end)
1
Put it in your local script DinozCreates 1070 — 5y
1
Also use local variables. DinozCreates 1070 — 5y
0
Use a bool value User#22788 5 — 5y
0
Use a bool value User#22788 5 — 5y
View all comments (2 more)
0
Use a bool value Donut792 216 — 5y
0
Use a bool value Donut792 216 — 5y

Answer this question