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

How exactly can I control what backspace does to a tool?

Asked by
shakran 23
7 years ago

Like I know if tool has canbedropped == true and you hit backspace itl just change location to game.Workspace while canbedropped == false itl go to backpack. I want to completly change what backspace does instead of doing any of these 2. Is this possible? For example hitting backspace will delete the tool itself completly

1 answer

Log in to vote
0
Answered by 7 years ago

Hello there. To answer your question, there are multiple ways to override the 2 functions you had mentioned. For this answer, I will use UserInputService. This service allows you to grab inputs from the keyboard. First, you should disable the CanBeDropped property from the tool.

local tool = script.Parent
tool.CanBeDropped = false

UserInputService can be grabbed from the game to allow us to use it. To get the service, we would use:

local UIS = game:GetService("UserInputService")

Now that we have the service, we would have a function that would tell the server what to do when the key Backspace is pressed. In this case, we will destroy the tool, meaning it will be removed from the player. (Note that it won't be removed from the StarterPack if it was placed there)

function onKeyPress(inputObject, gameProcessedEvent) -- Create function
    if inputObject.KeyCode == Enum.KeyCode.Backspace then -- If the player pressed Backspace
        tool:Destroy() -- Delete the tool
    end
end

Now that we have our function, we will create a way to call it using UserInputService.

UIS.InputBegan:connect(onKeyPress) -- UIS is the service variable we defined before. onKeyPress is the name of the function.

Now run the game and press Backspace. The tool will be removed from your player.

0
I know that much but what I want to know if if CanBeDropped == false wouldnt the ball go to backpack automatically once a player hits backspace? shakran 23 — 7y
0
It will, but it will also run the code above. PreciseLogic 271 — 7y
0
do you have an idea on which one will go first? shakran 23 — 7y
0
The function onKeyPress() will override the default function if CanBeDropped == false. Cowation 50 — 7y
View all comments (3 more)
0
Ohhhh alright sick. One more thing, when I tested this i made it print something instead of deleting the tool just for understanding but it printed the same thing like 200 times with 1 backspace. Is there anyway it can just print once like idk to look nice ig shakran 23 — 7y
0
Oh, I never tried print debugging, I'm still looking into UserInputService. I'll get back to you ASAP. Cowation 50 — 7y
0
ii thanks shakran 23 — 7y
Ad

Answer this question