Hello. I have made a hitbox system with the help of several forums and videos. It works okay for the most part, but it struggles to deal damage to the defender.
local script:
--[[ Varaibles ]]--
local UIS = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local AttackEvents = ReplicatedStorage:WaitForChild("AttackEvents")
---------------
--[[ Animations ]]--
local Player = game:GetService("Players").LocalPlayer or game:GetService("Players").PlayerAdded
local character = Player.Character or Player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local hum = character:WaitForChild("Humanoid")
local animator = hum:WaitForChild("Animator")
local anim = script:WaitForChild("PhantomAttackAnimation")
local PhantomAttackAnim = hum:LoadAnimation(anim)
---------------
--[[ Cooldowns and Debounces ]]
local AttackCooldown = false
--[[ Main ]]--
UIS.InputBegan:Connect(function(input, GPE)
if not GPE then
if input.UserInputType==Enum.UserInputType.MouseButton1 and AttackCooldown == false then
print("Working")
AttackCooldown = true
PhantomAttackAnim:Play()
PhantomAttackAnim.Looped = false
local hitbox = Instance.new("Part", workspace)
hitbox.Massless = true
hitbox.Size = Vector3.new(7.5,7.5,7.5)
hitbox.Shape = Enum.PartType.Ball
hitbox.CanCollide = false
hitbox.Transparency = .6
local weld = Instance.new("Weld", hitbox)
weld.Part0 = hrp
weld.Part1 = hitbox
for _,v in pairs (workspace:GetPartsInPart(hitbox)) do
if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
local hitbox = Instance.new(("IntValue"), v.Parent)
hitbox.Name = "Hit"..Player.Name
game.Debris:AddItem(hitbox, 1)
print("found someone")
AttackEvents:FindFirstChild("PhantomM1"):FireServer(v)
else
print("couldnt detect anyone")
AttackEvents:FindFirstChild("PhantomM1"):FireServer()
end
end
end
end
end)
AttackEvents.PhantomM1Cooldown.OnClientEvent:Connect(function(cooldownend)
print("Attack Available")
AttackCooldown = false
end)
--UIS.InputEnded:connect(function(input)
--if input.KeyCode==Enum.KeyCode.Q then
--AbilityEvents.PhantomWalkEvent:FireServer("Ended", script)
--end
--end)
Server script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Att = ReplicatedStorage:WaitForChild("AbilityEvents")
local AttackEvents = ReplicatedStorage:WaitForChild("AttackEvents")
local M1Sounds = ReplicatedStorage:WaitForChild("M1Sounds")
local TweenService = game:GetService("TweenService")
local cooldown = 1.5
local debounce = false
AttackEvents.PhantomM1.OnServerEvent:Connect(function(player, v)
if debounce == false then
debounce = true
print("server recieved attack event")
local PhantomM1Sound = M1Sounds.PhantomM1Sound:Clone()
PhantomM1Sound.Parent = player.Character.HumanoidRootPart
local RandomPitch = math.random(1,6)
if RandomPitch == 1 then
PhantomM1Sound.PlaybackSpeed = .9
elseif RandomPitch == 2 then
PhantomM1Sound.PlaybackSpeed = .95
elseif RandomPitch == 3 then
PhantomM1Sound.PlaybackSpeed = 1
elseif RandomPitch == 4 then
PhantomM1Sound.PlaybackSpeed = 1.05
elseif RandomPitch == 5 then
PhantomM1Sound.PlaybackSpeed = 1.1
elseif RandomPitch == 6 then
PhantomM1Sound.PlaybackSpeed = 1.15
end
PhantomM1Sound:Play()
for i,target in pairs (game.Players:GetPlayers()) do
print("hi1")
if target.Name ~= player.Name then
print("hi2")
if v ~= nil then
print("hi3")
if (v.Parent.HumanoidRootPart.Position - player.Character.HumanoidRootPart.Position).magnitude < 11 then
print("hi4")
v.Parent.Humanoid:TakeDamage(50)
end
end
end
end
wait(cooldown)
debounce = false
AttackEvents.PhantomM1Cooldown:FireClient(player)
end
end)
What I think the problem is is the server script. Sometimes, the damage will be dealt, and sometimes (most times) it won’t be dealt.
There is no error that shows up on the output.
I added lines of script that include prints so I could see where the script stops in its tracks. In the output, it prints out “hi1” and “hi2”, but does not display “hi3” or “hi4”. I believe I know the problem, but I do not know what to do from here. I have looked on other dev forums to see if I can solve the issue, but none of that have worked out for me.
Any help is appreciated. Thanks.