Currently struggling to figure out what is going on with the following script.
Here’s how it’s supposed to work:
if the left arm of the player is touched, then it searches for a humanoid. if theres no humanoid, then it doesnt continue the touched function. if there IS a humanoid, then it loops thru the players. if the touched object’s name is found in the loop of player names, then it recognizes the player, creates a variable for it(plr2), and checks for a couple values in that player. if they’re true, then it uses DMG1. if its false, but there’s still a player, then it’s supposed to only use DMG2. However, it uses DMG2 AND DMG3. DMG3 is reserved for models that aren’t players.
my code:
player.Character["Left Arm"].Touched:Connect(function(hit)
if player.punch_CD.Value == false then
player.punch_CD.Value = true
if hit.Parent:FindFirstChild("Humanoid") then
for i,v in pairs(game:GetService("Players"):GetChildren()) do
if hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hit.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
if v.Name == hit.Parent.Name then
local plr2 = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr2.MainStats.formType.Value == "UI" and plr2.Stats.inForm.Value == true then
local dmg1 = "Dodged!"
print("DMG1")
game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg1)
elseif plr2.Stats.inForm.Value == false then
print("DMG2")
local dmg2 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg2)
end
elseif v.Name ~= hit.Parent.Name then
local dmg3 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - dmg3
game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg3)
print("DMG3")
end
end
end
end
end)
Have you considered 2 parts hitting at the same time? Might be worth printing the “hit” part to see what is calling this function and how many times. These if statements are properly nested.
This means the same part is being checked twice, which means it’s the for loop, not the if loop. What I think you WANT to do for DMG3 is check if none of the players match.
This needs to be tested and debugged, because I’m a rookie… but something like this.
player.Character["Left Arm"].Touched:Connect(function(hit)
if player.punch_CD.Value == false then
player.punch_CD.Value = true
if hit.Parent:FindFirstChild("Humanoid") then
local hitTest = false
for i,v in pairs(game:GetService("Players"):GetChildren()) do
if hit.Parent.HumanoidRootPart:FindFirstChild("BodyVelocity") then
hit.Parent.HumanoidRootPart.BodyVelocity:Destroy()
end
if v.Name == hit.Parent.Name then
hitTest = true
local plr2 = game.Players:GetPlayerFromCharacter(hit.Parent)
if plr2.MainStats.formType.Value == "UI" and plr2.Stats.inForm.Value == true then
local dmg1 = "Dodged!"
print("DMG1")
game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg1)
elseif plr2.Stats.inForm.Value == false then
print("DMG2")
local dmg2 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg2)
end
end
end
if not hitTest then
local dmg3 = (0.1*(player.MainStats.Power.Value + 1))*player.Stats.formMulti.Value
hit.Parent.Humanoid.Health = hit.Parent.Humanoid.Health - dmg3
game.ReplicatedStorage.Attacks.Combat.dmgBoard:FireAllClients(hit.Parent, dmg3)
print("DMG3")
end
end
end)