Hey, I’m having a bit of trouble with my script. It’s a pretty simple script, I’m making a sound play when I click with RMB, it works, but, I want it to only execute when I’m clicking the button, because when I try to move the camera holding the RMB it still executes. I think I found a solution but it seems to be pretty filled with unnecessary things you know? Thanks for reading until here!
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local MouseEnum = Enum.UserInputType.MouseButton2
local Knife = script.Parent
local SndThrow = script.SndThrow
local function InputBegan(input, gameProcessed)
if input.UserInputType == MouseEnum and not UIS:IsKeyDown(MouseEnum) then
print("Right Click!")
SndThrow:Play()
end
end
UIS.InputBegan:Connect(InputBegan)
If you look at your InputBegan function there is a parameter called gameProcessed.
local function InputBegan(input, gameProcessed)
The gameProcessed is used for exactly these types of situations. It basically tells you if the player has actually clicked the button willingly. If they are typing in chat and typed in “g”, for example, gameProcessed would be true.
Still not working. I don’t want it to happen when I’m moving camera, sadly gameprocessed doesn’t works for that. Only if player is using the chat, etc.
Instead of doing all the main code in InputBegan, do it in InputEnded
Heres why:
If you calculate the time of when the player released the click(InputEnded) - when the player actually clicked(InputBegan), if your number is small enough then do said code, it would look something like this:
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local MouseEnum = Enum.UserInputType.MouseButton2
local Knife = script.Parent
local SndThrow = script.SndThrow
local ClickTime
local function InputBegan(input, gameProcessed)
if gameProcessed then return end --Always have this no matter what for input
if input.UserInputType == MouseEnum and not UIS:IsKeyDown(MouseEnum) then
ClickTime = os.clock()
--USE os.clock, it has numbers small enough to calculate how long a click would take
--os.time() gives you just seconds(integers not floats, aka bad)
end
end
local function InputEnded(input, gameProcessed)
if gameProcessed then return end --Always have this no matter what for input
if input.UserInputType == MouseEnum and not UIS:IsKeyDown(MouseEnum) and os.clock() - ClickTime <= (3/15) then --(3/15) is a generous click time, make the 15 larger if you want the click to be faster.
print("Right Click!")
SndThrow:Play()
end
end
UIS.InputBegan:Connect(InputBegan)
UIS.InputEnded:Connect(InputEnded)
Hey, thanks for the reply. I trying making a script based in this one you sent, but sadly, didn’t work. I tried copy and paste your script and that error appears:
Unable to cast token to token
Sometimes it appears at the line 10, sometimes at the line 20.
Anyways, thanks for the help! If you know how to fix this issue, please let me know.
You could use a boolean that is set to false and then set to true whenever and a player holds down a button and when the boolean is false. When the button is held up again it’s set to false.