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)