Script that Forcefully unequips a tool when a player types the key 1, in a textbox

Hey Roblox Developers, I’m currently working on a game called Writing which is inspired by the popular anime series Death Note. Currently I’m working on a script that allows players to pull out the Death note, write a player’s name down and kill that player.

I’m having trouble, however. The player needs to be able to press the 1 key at any point in the middle of writing the players name, so they can hide the death note in their inventory.

ContextActionService which I have been using to bind the key binds for durations while the book is equipped, or the player is doing something. but the specific special key, 1 is not working when using the service. So I believe by disabling the default roblox CoreGui control, I can rewire it so the 1 key will unequip the book and not type the number 1 instead. But I don’t know how. I’ve looked closely at the wiki, and a post a previous person made:


AwnswerBox:GetPropertyChangedSignal('Text'):Connect(function() -- Looks for if they have typed in the textbox.
	local match = string.match(AwnswerBox.Text, "%d") -- checks if they have typed any numbers in the box.
	if match ~= nil and match == '1' then --if it finds a matching number and that number is 1 which has been typed.
		AwnswerBox:ReleaseFocus() -- Forces the player to stop typing.
		
		--Unequip the deathnote forefully----
		--Confused on what I should do from here----
	end
	
end)

Entire Script and Workspace

–WritingScript

local ContextActionService = game:GetService('ContextActionService')
local UserInputService = game:GetService('UserInputService')

local NotebookScreen = script.Parent.Parent.Parent
local AwnswerBox = script.Parent
local player = game.Players.LocalPlayer


local PressedEnter = 'Enter'
local PressedOne = 'One'


function handleAction(actionName, inputState, _inputObject) -- Handles Submiting Usernames, using Enter. 
	
	if actionName == PressedEnter and inputState == Enum.UserInputState.Begin then --Checks if the action that happend was indeed ENTER, and the player has started inputing that key.
		print('Enterd Name!')
	end
end


AwnswerBox:GetPropertyChangedSignal('Text'):Connect(function() -- Looks for if they have typed in the textbox.
	local match = string.match(AwnswerBox.Text, "%d") -- checks if they have typed any numbers in the box.
	if match ~= nil and match == '1' then --if it finds a matching number and that number is 1 which has been typed.
		AwnswerBox:ReleaseFocus() -- Forces the player to stop typing.
		
		--Unequip the deathnote forefully, Final part of script.
		--Confused on what to do.---
	end
	
end)

AwnswerBox.FocusLost:Connect(function(IsEnterPressed, InputThatReleasedFocus) -- Will instantly run when the player presses Enter
	
	if IsEnterPressed then
		handleAction(PressedEnter, Enum.UserInputState.Begin)
	end
end)


NotebookScreen:GetPropertyChangedSignal('Enabled'):Connect(function() -- Will instantly run whenever the player presses enter and they are on the screen
	if NotebookScreen.Enabled == true then
		AwnswerBox:CaptureFocus() -- Forces the player to begin Writing.
		ContextActionService:BindAction(PressedEnter, handleAction, true, Enum.KeyCode.Return) 
		
		
	else
		ContextActionService:UnbindAction(PressedEnter) -- Unbinds the enterfunction.
	end
end)

–Edit made simpler to read. I will most likely take a peak back Tomorrow are all these posts.

2 Likes

My script is currently incomplete. because the 1 key must work before, I can continue.

1 Like

You can do a variety of things. I messed around with it and came up with the following:

local AnswerBox = script.Parent
local Players = game:GetService(“Players”)
local plr = Players.LocalPlayer
local character = plr.Character

AnswerBox:GetPropertyChangedSignal(‘Text’):Connect(function()
print(“Text Changed”)
local match = string.match(AnswerBox.Text, “%d”)
if match ~= nil and match == ‘1’ then
AnswerBox:ReleaseFocus()
local humanoid = character:FindFirstChild(“Humanoid”)
if humanoid then
humanoid.Health = 0
end
end
end)

I put the book in Replicated Storage and cloned it into workspace when player adds into the game.
this goes into Server Script Service
game.Players.PlayerAdded:Connect(function()
local book = game.ReplicatedStorage.Book
local newbook = book:Clone()
newbook.Parent = game.Workspace
newbook.Name = “book”
end)

& I put this script inside the book Tool:
local book = game.Workspace:WaitForChild(“book”)
book.Equipped:Connect(function()
local Character = book.Parent
if Character then
local Player = game.Players:GetPlayerFromCharacter(Character)
if Player then
local PlayerGui = Player:WaitForChild(“PlayerGui”)
local sgui = PlayerGui.ScreenGui
sgui.Enabled = true
end
end
end)

I’m pretty sure you can’t get anything written in a textbox until you have lost focus, then it can get the string written inside. Might be wrong though.

I appeciate your look into my programming. However, I have already programmed this to work in the game. You can enter a username, and kill the player.

What I am trying to do is, when the player is in the middle of typing the players name, and they type the letter 1, It does not print 1 to the textbox. rather it closes the interface and cancels everything instantly.

AwnswerBox:GetPropertyChangedSignal('Text'):Connect(function() -- Looks for if they have typed 
   Answer
end)

No it’s possible, this event fires everytime the property ‘text’ of textbox is changed. By simply doing
Awnser.Text you get the text everytime a player types something.

Here’s what I would do to unequip the tool no matter what

local ContextActionService = game:GetService('ContextActionService')
local UserInputService = game:GetService('UserInputService')

local NotebookScreen = script.Parent.Parent.Parent
local AwnswerBox = script.Parent
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()

local PressedEnter = 'Enter'

-- Handles pressing Enter to confirm the name
function handleAction(actionName, inputState, _inputObject)
	if actionName == PressedEnter and inputState == Enum.UserInputState.Begin then
		print('Entered Name!')
	end
end

-- Handles when text is typed
AwnswerBox:GetPropertyChangedSignal('Text'):Connect(function()
	local match = string.match(AwnswerBox.Text, "%d") -- check for number
	if match ~= nil and match == '1' then
		print("Detected forbidden key: 1") -- Here is the main part of the script. I used .Text to catch the change where CAS can't

		AwnswerBox.Text = "" -- clear the box
		AwnswerBox:ReleaseFocus() -- stop typing
		NotebookScreen.Enabled = false -- close the notebook

		-- Force unequip the tool when 1 writtten
		local tool = character:FindFirstChildOfClass("Tool")
		if tool then
			tool.Parent = player.Backpack
		end
	end
end)

-- Handles pressing Enter
AwnswerBox.FocusLost:Connect(function(IsEnterPressed, InputThatReleasedFocus)
	if IsEnterPressed then
		handleAction(PressedEnter, Enum.UserInputState.Begin)
	end
end)

-- When screen is shown, capture focus and bind Enter
NotebookScreen:GetPropertyChangedSignal('Enabled'):Connect(function()
	if NotebookScreen.Enabled == true then
		AwnswerBox.Text = "" -- clear previous input
		AwnswerBox:CaptureFocus()
		ContextActionService:BindAction(PressedEnter, handleAction, true, Enum.KeyCode.Return) 
	else
		ContextActionService:UnbindAction(PressedEnter)
	end
end)

Although this might be counterintuitive as what if a player has a 1 in their name?

1 Like

Didn’t know that was possible, thats quite cool. Thanks :slight_smile:

1 Like

ahh ok, i mis understood what you were trying to do.

1 Like

Thank you so much! The Forbidden key 1, now closes the notebook even if the player is typing in the textbox! Also I have custom names in the game to prevent that from happening and for game balance, so nobody can have the letter 1 in their name.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.