In my game I have a script that allows you to reset your character after pressing R.
The only problem is that when chatting it resets your character after typing R.
Is there any way I can disable the script when opening the chat and re-enable it upon closing it.
Many Thanks for your help
Here is the script:
local character = player.Character
local enabled = true
local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:connect(function(input)
if input.KeyCode == Enum.KeyCode.R and enabled then
character.Head:Remove()
enabled = false
warn(player.Name.." has been reset successfully!")
wait(6)
enabled = true
end
end)
Yeah, we all faced the same issue, a keybind causing an action and overriding other keybinds.
You can simply fix this by using ContextActionService
This allows to create keybinds that only work under special conditions or certain period of times, for example if I set R to reset, by default ContextAction won’t do what I set my keybind to do because the chat is open, more info on that on the link above.
Really useful and recommended in such situations like yours.
InputBegan’s second argument is a boolean telling if the game already processed the input. I added a parameter for it and added it to the if statetement. Does it now work?
local character = player.Character
local enabled = true
local userinputservice = game:GetService("UserInputService")
userinputservice.InputBegan:connect(function(input, gameProcessedEvent)
if input.KeyCode == Enum.KeyCode.R and enabled and not gameProcessedEvent then
character.Head:Remove()
enabled = false
warn(player.Name.." has been reset successfully!")
wait(6)
enabled = true
end
end)
--Client
UIS.InputBegan:Connect(function(input, gameProcessedEvent)
if not gameProcessedEvent then
if input.KeyCode == Enum.KeyCode[ResetKeyBind] then
game.ReplicatedStorage.PlayerReset:FireServer()
end
end
end)
--Server:
game.ReplicatedStorage.PlayerReset.OnServerEvent:Connect(function(player)
player:LoadCharacter()
end)
This is a client-sided script, under StarterPlayer.StarterCharacterScripts.
local character = player.Character
local db = true
local UIS = game:GetService("UserInputService")
UIS .InputBegan:connect(function(input, gameProcessed)
if gameProcessed then return end
if input.KeyCode == Enum.KeyCode.R and db and character:FindFirstChild("Humanoid") and character.Humanoid.Health ~= 0 then
character.Humanoid.Health = 0
db = false
end
end)