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

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

Asked by 4 years ago

I Can get the activate event to fire after pressing M for 3 seconds but I cant Fire the Deactivate event after Pressing M for a second time

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)

2 answers

Log in to vote
0
Answered by 4 years ago
local Deactivate = Replicated:WaitForChild("Stop")

Just a guess before I attempt to help you any further, you identified a remote event named "Deactivate" but fired the "Use" event instead at line 37.

Also, may I see the local scripts so I can piece everything together?

                **Event:FireServer() -- fires the remote if they did** 
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

The Use event is fired after the player holds down the M Key after the 3 seconds have passed Also That is the Entire Local Script

Answer this question