So I’m making a combat system, but even though the player isn’t clicking it still does damage for some reason.
Script that detects the Mouse:
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local Event = script:WaitForChild("RemoteEvent")
local Script = script.Script
Script.Disabled = true
Mouse.Button1Down:Connect(function()
Event:FireServer(Player)
Script.Disabled = false
wait(1.5)
Script.Disabled = true
end)
Script that detects when the sever event is fired:
local Event = script.Parent:WaitForChild("RemoteEvent")
local CombatCount = 1
local Cooldown = false
Event.OnServerEvent:Connect(function(Player)
local Animations = {
RightPunch = script.Parent.Animations:WaitForChild("RightPunch"),
LeftPunch = script.Parent.Animations:WaitForChild("LeftPunch"),
RightKick = script.Parent.Animations:WaitForChild("RightKick")
}
local function RightTouched()
script.Parent.Parent.RightHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(20)
Cooldown = true
wait(1)
Cooldown = false
end
end)
end
local function LeftTouched()
script.Parent.Parent.LeftHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(20)
Cooldown = true
wait(1)
Cooldown = false
end
end)
end
local function FootTouched()
script.Parent.Parent.RightFoot.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(25)
Cooldown = true
wait(1)
Cooldown = false
end
end)
end
if CombatCount == 1 and Cooldown == false then
RightTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.RightPunch)
RightPunch:Play()
CombatCount = 2
Cooldown = true
wait(0.1)
Cooldown = false
elseif CombatCount == 2 and Cooldown == false then
LeftTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.LeftPunch)
RightPunch:Play()
CombatCount = 3
Cooldown = true
wait(0.1)
Cooldown = false
elseif CombatCount == 3 and Cooldown == false then
RightTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.RightPunch)
RightPunch:Play()
CombatCount = 4
Cooldown = true
wait(0.1)
Cooldown = false
elseif CombatCount == 4 and Cooldown == false then
FootTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.RightKick)
RightPunch:Play()
CombatCount = 1
Cooldown = true
wait(0.1)
Cooldown = false
end
end)
Can’t you just Disconnect() the function? That’s a whole lot of custom functions that you’re handling here, I don’t think it’s really that necessary to include that much
On you script that detects when server event is fired do:
local Event = script.Parent:WaitForChild("RemoteEvent")
local CombatCount = 1
local Cooldown = false
Event.OnServerEvent:Connect(function(Player)
local Animations = {
RightPunch = script.Parent.Animations:WaitForChild("RightPunch"),
LeftPunch = script.Parent.Animations:WaitForChild("LeftPunch"),
RightKick = script.Parent.Animations:WaitForChild("RightKick")
}
local function RightTouched()
script.Parent.Parent.RightHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(0)
Cooldown = true
wait(1)
Cooldown = false
end
end)
end
local function LeftTouched()
script.Parent.Parent.LeftHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(0)
Cooldown = true
wait(1)
Cooldown = false
end
end)
end
local function FootTouched()
script.Parent.Parent.RightFoot.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(0)
Cooldown = true
wait(1)
Cooldown = false
end
end)
end
if CombatCount == 1 and Cooldown == false then
RightTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.RightPunch)
RightPunch:Play()
CombatCount = 2
Cooldown = true
wait(0.1)
Cooldown = false
elseif CombatCount == 2 and Cooldown == false then
LeftTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.LeftPunch)
RightPunch:Play()
CombatCount = 3
Cooldown = true
wait(0.1)
Cooldown = false
elseif CombatCount == 3 and Cooldown == false then
RightTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.RightPunch)
RightPunch:Play()
CombatCount = 4
Cooldown = true
wait(0.1)
Cooldown = false
elseif CombatCount == 4 and Cooldown == false then
FootTouched()
local RightPunch = Player.Character.Humanoid:LoadAnimation(Animations.RightKick)
RightPunch:Play()
CombatCount = 1
Cooldown = true
wait(0.1)
Cooldown = false
end
end)
Your nested functions (Or your function inside a function)
You see, when you call a specific function inside a Event, it will keep detecting when that hit.Parent is found & will keep resuming to damage them non-stop (Even when you disable the Script, which wouldn’t even work anyways as you’re disabling it on the client & not on the server)
What you could do, is connect & disconnect your functions whenever they’re called on so that you don’t have to worry about it detecting again when you call Disconnect()
local Part = script.Parent
local Connection
local function DetectTouch(Hit)
print("I've been hit! I will now cry :(")
Connection:Disconnect()
end
Connection = Part.Touched:Connect(DetectTouch)
This print statement in this example would only fire once, as you disconnected the function when the Event fired itself so it’s not gonna fire again another time unless you create local Connection on it again
You will need to disconnect the touched functions which would look something like:
local connection
connection = Part.Touched:Connect(function(Part)
if part.Parent:FindFirstChild("Humanoid") then
part.Parent.Humanoid:TakeDamage(20)
connection:Disconnect()
end
end)
local Connection = nil
local Debounce = false
Connection = game.Workspace.Part.Touched:Connect(function(Part)
if Part.Parent:FindFirstChild("Humanoid") and Debounce == false then
Debounce = true
print(1)
wait(2)
Connection:Disconnect()
print(2)
Debounce = false
end
end)
And it prints(1) and prints(2) once then doesnt print again
I think you need to define the variable for some reason as nil then make the variable the function and disconnect it that way, which is a bit weird
Hmm this should work in my mind, but it says: Workspace.Rapideed.LocalScript.Script:30: attempt to index nil with 'Disconnect'
sorry for wasting your time
local Event = script.Parent:WaitForChild("RemoteEvent")
local Connection = nil
Event.OnServerEvent:Connect(function(Player)
local function RightTouched()
local Connection = script.Parent.Parent.RightHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(20)
Cooldown = true
wait(1)
Cooldown = false
Connection:Disconnect()
end
end)
end
local function LeftTouched()
local Connection = script.Parent.Parent.LeftHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(20)
Cooldown = true
wait(1)
Cooldown = false
Connection:Disconnect()
end
end)
end
local Event = script.Parent:WaitForChild("RemoteEvent")
local Connection = nil
Event.OnServerEvent:Connect(function(Player)
local function RightTouched()
local Connection = nil
Connection = script.Parent.Parent.RightHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(20)
Cooldown = true
wait(1)
Cooldown = false
end
end)
wait(2) --I'd recommend to make a wait here for the duration of the kick or however you want
Connection:Disconnect()
end
local function LeftTouched()
local Connection = nil
Connection = script.Parent.Parent.LeftHand.Touched:Connect(function(hit)
if Cooldown == false then
local TargetHumanoid = hit.Parent:FindFirstChild("Humanoid")
if script.Parent.Parent.Name == hit.Parent.Name then return end
TargetHumanoid:TakeDamage(20)
Cooldown = true
wait(1)
Cooldown = false
end
end)
wait(2)
Connection:Disconnect()
end
For the wait you can just do wait(RightPunch.Length) etc for each type of animation
Did it error again? Because another alternative which I don’t recommend is just making a variable at the start of the script that is false for AttackEnded then true and on the .Touched just do at the start like
if Cooldown == false and AttackEnded == false then
end