UIS firing from other inputs as well as the key I selected

I’m using a count system to detect if the key was pressed twice and when my count reaches 1 off the first input, any other input will raise the count even though I have it set to only V. Is this a issue with my code or a uis bug?

local plr = game.Players.LocalPlayer
local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local humRP = char:WaitForChild("HumanoidRootPart")

local RS = game:GetService("ReplicatedStorage")
local Debris = game:GetService("Debris")
local UIS = game:GetService("UserInputService")
local TS = game:GetService("TweenService")
local RunService = game:GetService("RunService")

local debounce = false
local CD = 10
local count = 0

local tpRemote = RS.Remotes.Teleport

local mouse = plr:GetMouse()

local KEY = Enum.KeyCode.V

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}

local camera = workspace.CurrentCamera

local kunaiClone = nil

local function throwKunai()
	
	local rayMaxDist = 1500
	local mousePos = UIS:GetMouseLocation()
	local rayOrigin = camera:ViewportPointToRay(mousePos.X, mousePos.Y)

	local raycastResult = workspace:Raycast(rayOrigin.Origin, rayOrigin.Direction * rayMaxDist)
	

	if raycastResult == nil then print("nil")
		return
	end
	
	local kunaiPosition = humRP.Position
	
	kunaiClone = RS.FX.RaijinKunai:Clone()
	kunaiClone.Parent = workspace.Map.Ignore
	kunaiClone.CanCollide = false
	kunaiClone.Anchored = true
	kunaiClone.CFrame = CFrame.lookAt(kunaiPosition, raycastResult.Position)  * CFrame.Angles(0, math.rad(-90), math.rad(90))
	-- Look at creates a CFrame that starts at a first position and points towards a second
	-- To flip the direction, multiply the CFrame.lookAt result by CFrame.Angles(0, math.rad(180), 0)

	local distance = (raycastResult.Position - kunaiPosition).Magnitude -- (Target Position - Object Position)
	local speed = 150
	
	local tweenTime = distance / speed
	
	local objHit = raycastResult.Instance
	
	local tweenInfo = TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
	local kunaiDirection =  {Position = raycastResult.Position}
	local tweenKunai = TS:Create(kunaiClone, tweenInfo, kunaiDirection)

	tweenKunai:Play()
	
	print(objHit)
	print("Pressed V")

end


UIS.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	if inp.KeyCode == KEY and not debounce or count == 1 then
		debounce = true
		count += 1
		if count <= 1 then
			throwKunai()
		end
		if count >= 2 then
			if kunaiClone then
				tpRemote:FireServer(kunaiClone.Position, kunaiClone:Destroy())
			end
			count = 0
			print("reset count")
		end
		print(count)
		task.wait(CD)
		debounce = false
	end
end)

If you’re VERY sure that it’s firing when you’re pressing something else other than V, and no other scripts are interfering with this, then it might be a UserInputService issue.

I would suggest using ContextActionService instead of UserInputService, since it is a better choice for making your system compatibility supported (phone, tablet).

I’m 1000% sure it’s when I give it any type of input, ill try ContextActionService, thanks. :+1:

2 Likes

Idk why you need this remove it. You’re already checking inside the statement anyway

if inp.KeyCode == KEY and not debounce or count == 1 then

I’m pretty sure the error may be happening here, due to the or statement.
running the following code:

if false and not true or true then print("fart") end

will result in fart being printed. Meaning that as long as count == 1 it will accept any keyboard input.
This can be fixed by wrapping not debounce or count == 1 with ().

resulting in

if inp.KeyCode == KEY and (not debounce or count == 1) then

which when applied to the barebones example, no longer prints fart.

2 Likes

thanks for the help, fart was a great example btw

1 Like

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