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

I can't get the deactivate event to fire What am i doing wrong?

The inital Script would run smoothly after the 3 seconds is held for the M Key but when i press it again to deactivate it doesnt fire the Stop Event

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

Where is your script/the remotes located?

1 answer

✓ Accepted answer

This code seems to run for me: I got rid of your while loop and instead keep track of time using tick() since tick() returns the current time.

local player = game:GetService("Players").LocalPlayer

local UseEvent = script.Parent:WaitForChild("Use")
local DeactivateEvent  =  script.Parent:WaitForChild("Stop")

local Key = "M" 
local startHoldingTime = tick() -- time when the player started holding. tick() is the current time
local expectedHoldTime = 3 -- How long you want the player to be hold the key for

local isBeingUsed = false

-- runs whenever the player presses a key
game:GetService("UserInputService").InputBegan:Connect(function(Input, gameProcessed)
    if gameProcessed then return end -- stops this event from firing if we're typing / roblox is using the keys in some way

    if Input.KeyCode == Enum.KeyCode[Key] then 
        startHoldingTime = tick()
    end
end)

--runs whenever the player stops pressing a key
game:GetService("UserInputService").InputEnded:Connect(function(Input, gameProcessed) 
    if gameProcessed then return end -- stops this event from firing if we're typing / roblox is using the keys in some way

    local timeHeldDown = tick() - startHoldingTime --how long the player held down the button in seconds

    if Input.KeyCode == Enum.KeyCode[Key] then
 		print(player.Name..' holded '..Key..' for '.. timeHeldDown ..' second(s).')
		
		--if the player is using the mode already, fire the deactivate event
		if isBeingUsed then
			print("Firing the deactivate event!")
            DeactivateEvent:FireServer()
			isBeingUsed = false
		--if the player HASN'T used the mode yet, check if they held the key long enough to use the mode
		else
	        if timeHeldDown >= expectedHoldTime then 
				print("Firing the used event!")
				isBeingUsed = true
	            UseEvent:FireServer()
	        else
				print("player didn't hold key for long enough to fire any events. ")
	        end	
		end
    end
end)


if this code doesn't work, make sure you have your events on the server working should look something like this:

local UseEvent = location of the use event
local DeactivateEvent = location of the deactivate event

UseEvent.OnServerEvent:Connect(function(player)
	print(player.Name .. " fired the use event!") 
end)

DeactivateEvent.OnServerEvent:Connect(function(player)
	print(player.Name .. " fired the use decativate event!") 
end)

4 comments

0

just saw you went online, everything alright? let me know if it's not working

0

testing it right now

0

sure thing.

0

I edited the script to fire the deactivate event if the player has not held the remote down for the seconds if that is what you wanted. If you're still stuck, maybe we can talk this out through discord? Lucy#4854 

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