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

How to use the same key twice?

Hello everyone, Its currently very late but I want to finish this before I go to sleep.

So I have a script where a player prints out "In" whenever they press H. However, with the else statement when I press H again it should print "Out". Could someone help me out with this because i'm missing something very simple or im just overthinking something. Thanks.

Local Script:

local REMOTES = game.ReplicatedStorage:WaitForChild("Events")

local uis = game:GetService("UserInputService");

----------------- Remote Function ----------------------

function UIS(key) -- Function for UserInputService
    if key.KeyCode == Enum.KeyCode.H then
        REMOTES.Printing:FireServer();
    end 
end 

uis.InputBegan:Connect(UIS); 

Server Script:

local REMOTES = game.ReplicatedStorage:WaitForChild("Events")

REMOTES.Printing.OnServerEvent:Connect(function(player)
local character	= player.Character
	if character then 
		print("In")
	else
		print("Out")
		

	end
end)

1 answer

✓ Accepted answer
--Local script
local REMOTES = game.ReplicatedStorage:WaitForChild("Events")
local uis = game:GetService("UserInputService");
local bool = false

function UIS(key) -- Function for UserInputService
    if key.KeyCode == Enum.KeyCode.H then
     if bool ~= false then
        bool = not bool
        REMOTES.Printing:FireServer(true);
        else
        if bool ~= true then
	    bool = not bool
        REMOTES.Printing:FireServer(false);
    end 
end 
end
end

uis.InputBegan:Connect(UIS); 

I edited both of them so the should work now

--Workspace script.
local REMOTES = game.ReplicatedStorage:WaitForChild("Events")

REMOTES.Printing.OnServerEvent:Connect(function(player,bool)
if bool == true then 
        print("In")
    elseif bool == false then
        print("Out")
    end
end)

1 comment

0

Thank you!

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