You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
Trying to remake Strongest Battlegrounds M1s and they work fine but if i hold down for too long the m1s will keep going even when i release my mouse.
2. What is the issue? Include screenshots / videos if possible!
If I hold down M1s for too long and then release MouseButton1Up/UserInputService won’t fire
3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
I looked on the devforum but couldn’t find any solutions
-- M1s
local mouse = LocalPlayer:GetMouse()
local m1Event = game.ReplicatedStorage:WaitForChild('M1')
local down = false
local UIS = game:GetService('UserInputService')
local function DoM1()
if down then
m1Event:FireServer()
end
end
local connection
local function GetDown()
connection = game:GetService('RunService').RenderStepped:Connect(function()
DoM1()
task.wait()
end)
end
UIS.InputEnded:Connect(function(object)
if object.UserInputType == Enum.UserInputType.MouseButton1 then
print('Up')
down = false
connection:Disconnect()
end
end)
mouse.Button1Down:Connect(function()
down = true
GetDown()
end)
It looks like the problem was that you were calling task.wait() inside of RunService.RenderStepped. This will stop your code, if you were to lift your finger off of MouseButton1 while the code was stopped (which it should be most of the time since RunService.RenderStepped fires once per frame), the code would not register that.
Since RunService.RenderStepped runs once per frame, it does not need a task.wait().
I have edited the code a little bit to make it work. I also made it slightly more efficient:
-- M1s
local mouse =game:GetService("Players").LocalPlayer:GetMouse()
local m1Event = game.ReplicatedStorage:WaitForChild('M1')
local down = false
local cooldown = 0.2
local timeDown = 0
local UIS = game:GetService('UserInputService')
local function DoM1()
if down then
print("Fired")
m1Event:FireServer()
end
end
local connection
game:GetService('RunService').RenderStepped:Connect(function(delta)
timeDown += delta
if timeDown >= cooldown then
timeDown = 0
DoM1()
end
end)
UIS.InputEnded:Connect(function(object)
if object.UserInputType == Enum.UserInputType.MouseButton1 then
down = false
end
end)
mouse.Button1Down:Connect(function()
down = true
end)