I’m writing a script that allows players to acquire punches if they click within the hitbox, however even after I disconnect the function, if the user still clicks in the hitbox after they stop touching it, punches continue to be added. heres the scripts
local script
local player = game:GetService('Players').LocalPlayer
local char = player.CharacterAdded:Wait() or player.Character
local humanoid = char:WaitForChild('Humanoid')
local mouse = player:GetMouse()
local repStorage = game:GetService('ReplicatedStorage')
local Remotes = repStorage:WaitForChild('Remotes')
local folder = workspace:WaitForChild('PunchingBags') -- Replace 'Punching Bags' with the actual name of the folder containing the punching bags
local debounce = false -- Create a debounce variable to track if the player can punch
local connection
for i, v in pairs(folder:GetChildren()) do
if v:IsA('Model') then
local Hitbox = v:WaitForChild('Hitbox')
Hitbox.Touched:Connect(function(touch)
local humanoid = touch.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
connection = mouse.Button1Down:Connect(function()
if not debounce then
debounce = true
Remotes.Punched:FireServer(player)
wait(0.15) -- Adjust the duration based on your desired debounce time
debounce = false
end
end)
Hitbox.TouchEnded:Connect(function()
connection:Disconnect()
end)
end
end)
end
end
Script
local repStorage = game:GetService('ReplicatedStorage')
local Remotes = repStorage:WaitForChild('Remotes')
for i,v in pairs(script.Parent:GetChildren()) do
if v:IsA('Model') then
if v:FindFirstChild('Increase') then
local Increase = v:WaitForChild('Increase')
Remotes.Punched.OnServerEvent:Connect(function(player)
player:WaitForChild("leaderstats").Punches.Value = player:WaitForChild("leaderstats").Punches.Value + Increase.Value
end)
end
end
end
Connections have been acting a little weird lately, but maybe check if the connection is connected like this?
if connection.Connected then
connection:Disconnect()
end
also you dont have to add a “player” variable here since serverscripts can already make the player the first variable inside of a remote event function
This worked though when im using touchended its not working. I also added a print to check if its detecting when the touch ends and that worked but the connection isnt disconnecting
local player = game:GetService('Players').LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild('Humanoid')
local mouse = player:GetMouse()
local repStorage = game:GetService('ReplicatedStorage')
local Remotes = repStorage:WaitForChild('Remotes')
local folder = workspace:WaitForChild('PunchingBags') -- Replace 'PunchingBags' with the actual name of the folder containing the punching bags
local debounce = false -- Create a debounce variable to track if the player can punch
local connection
for i, v in pairs(folder:GetChildren()) do
if v:IsA('Model') then
local Hitbox = v:WaitForChild('Hitbox')
Hitbox.Touched:Connect(function(touch)
local humanoid = touch.Parent:FindFirstChildOfClass('Humanoid')
if humanoid then
connection = mouse.Button1Down:Connect(function()
if not debounce then
debounce = true
Remotes.Punched:FireServer(player)
wait(0.15) -- Adjust the duration based on your desired debounce time
debounce = false
end
end)
local touchEndedConn
touchEndedConn = Hitbox.TouchEnded:Connect(function()
if connection and connection.Connected then
connection:Disconnect()
end
touchEndedConn:Disconnect()
end)
end
end)
end
end
made a local variable to see if it fix and lets me know if it tells any error
Does maybe adding a bool value to make it where the player can and cannot touch the hitbox maybe work out?
local player = game:GetService('Players').LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild('Humanoid')
local mouse = player:GetMouse()
local repStorage = game:GetService('ReplicatedStorage')
local Remotes = repStorage:WaitForChild('Remotes')
local folder = workspace:WaitForChild('PunchingBags') -- Replace 'PunchingBags' with the actual name of the folder containing the punching bags
local debounce = false -- Create a debounce variable to track if the player can punch
local cantouch = false -- Tells you if you can touch the part or not
local connection
for i, v in pairs(folder:GetChildren()) do
if v:IsA('Model') then
local Hitbox = v:WaitForChild('Hitbox')
Hitbox.Touched:Connect(function(touch)
local humanoid = touch.Parent:FindFirstChildOfClass('Humanoid')
if humanoid then
connection = mouse.Button1Down:Connect(function()
if not debounce and cantouch ~= true then
cantouch = true
debounce = true
Remotes.Punched:FireServer(player)
wait(0.15) -- Adjust the duration based on your desired debounce time
debounce = false
end
end)
local touchEndedConn
touchEndedConn = Hitbox.TouchEnded:Connect(function()
if connection and connection.Connected and cantouch ~= false then
cantouch = false
connection:Disconnect()
end
touchEndedConn:Disconnect()
return connection
end)
end
end)
end
end
Maybe edit this part and replace it with this instead? im not too sure
touchEndedConn = Hitbox.TouchEnded:Connect(function()
if connection and connection.Connected and cantouch ~= false then
cantouch = false
debounce = false
connection:Disconnect()
end
touchEndedConn:Disconnect()
return connection
end)
if this doesn’t work then i can’t really help you today , sorry i’m pretty sleepy so not really concentrated
local player = game:GetService('Players').LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local humanoid = char:WaitForChild('Humanoid')
local mouse = player:GetMouse()
local repStorage = game:GetService('ReplicatedStorage')
local Remotes = repStorage:WaitForChild('Remotes')
local folder = workspace:WaitForChild('PunchingBags') -- Replace 'PunchingBags' with the actual name of the folder containing the punching bags
local debounce = false -- Create a debounce variable to track if the player can punch
local connection
for i, v in pairs(folder:GetChildren()) do
if v:IsA('Model') then
local Hitbox = v:WaitForChild('Hitbox')
Hitbox.Touched:Connect(function(touch)
local humanoid = touch.Parent:FindFirstChildOfClass('Humanoid')
if humanoid then
connection = mouse.Button1Down:Connect(function()
if not debounce then
debounce = true
Remotes.Punched:FireServer(player)
wait(0.15) -- Adjust the duration based on your desired debounce time
debounce = false
end
end)
local touchEndedConn
touchEndedConn = Hitbox.TouchEnded:Connect(function(touchEnded)
if touchEnded.Parent == touch.Parent then
if connection and connection.Connected then
connection:Disconnect()
end
touchEndedConn:Disconnect()
end
end)
end
end)
end
end
No, the connection still dosen’t disconnect and there’s no errors at all.Also, when i print the connection after disconnecting it prints out “Connection” same thing if i print out the touchEndedConn