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

How do i fire the deactivate event after the Activate Event has been fired?

So i figured out to fire the Use Event but i cant get it to deactive

local PlayerService = game:GetService("Players")
local UserInputService = game:GetService("UserInputService") -- Using UserInputService instead of mouse.

local Client = PlayerService.LocalPlayer

local Key = "M" -- The ke the person has to hold

local Holding = false -- Detects when the key is being held
local ExpectedHold = 3 -- How long you want the player to be hold the key for
local HoldingTime = 0 -- The amount of seconds they holded for
local Replicated = script.Parent
local Event = Replicated:WaitForChild("Use")
local Deactivate = Replicated:WaitForChild("Stop")

UserInputService.InputBegan:Connect(function(Input, GPE)
    if not GPE then
        if Input.KeyCode == Enum.KeyCode[Key] then -- When they start to press the key this runs
            if not Holding then -- if they arent already holding then it continues
                Holding = true -- Makes holding true/script now knows they're holding the key

                while Holding do wait(1) -- Basically while holding == true the code after this will run every second
                    if HoldingTime < ExpectedHold then
                        HoldingTime = HoldingTime + 1 -- This adds to how long they holder the key for
                    end
                end
            end
        end
    end
end)


UserInputService.InputEnded:Connect(function(Input, GPE) -- runs when they stop holding a key
    if not GPE then
        if Input.KeyCode == Enum.KeyCode[Key] then
            if HoldingTime >= ExpectedHold then -- if they holding the key for the right amount of time
                Event:FireServer() -- fires the remote if they did
            else
                print(Client.Name..' holded '..Key..' for '..HoldingTime..' second(s).')
                HoldingTime = 0 -- sets the time back to 0
            end
        end
    end
end)

1 answer

Holding never gets set to false therefore the while loop keeps going as it has never been told to stop, correct me if I'm wrong. corrected code (hopefully works)

local PlayerService = game:GetService("Players")
local UserInputService = game:GetService("UserInputService") -- Using UserInputService instead of mouse.

local Client = PlayerService.LocalPlayer

local Key = "M" -- The ke the person has to hold

local Holding = false -- Detects when the key is being held
local ExpectedHold = 3 -- How long you want the player to be hold the key for
local HoldingTime = 0 -- The amount of seconds they holded for
local Replicated = script.Parent
local Event = Replicated:WaitForChild("Use")
local Deactivate = Replicated:WaitForChild("Stop")

UserInputService.InputBegan:Connect(function(Input, GPE)
    if not GPE then
        if Input.KeyCode == Enum.KeyCode[Key] then -- When they start to press the key this runs
            if not Holding then -- if they arent already holding then it continues
                Holding = true -- Makes holding true/script now knows they're holding the key

                while Holding do wait(1) -- Basically while holding == true the code after this will run every second
                    if HoldingTime < ExpectedHold then
                        HoldingTime = HoldingTime + 1 -- This adds to how long they holder the key for
                    end
                end
            end
        end
    end
end)


UserInputService.InputEnded:Connect(function(Input, GPE) -- runs when they stop holding a key
    if not GPE then
        if Input.KeyCode == Enum.KeyCode[Key] then
	  Holding = false
            if HoldingTime >= ExpectedHold then -- if they holding the key for the right amount of time
                Event:FireServer() -- fires the remote if they did
            else
                print(Client.Name..' holded '..Key..' for '..HoldingTime..' second(s).')
                HoldingTime = 0 -- sets the time back to 0
            end
        end
    end
end)

1 comment

0

Nope didn't work i'm trying to get it if you press M again it fires the Deactivate Event

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