Making a weapon hitbox that only detect enemies

I got currently two scripts. One local that fires the RemoteEvent and one normal one.

At the moment I got issues detecting if the char in the hitbox is a enemy or not before damaging it. I assume that it detects the localplayer and does not search for anything else in the hitbox. If anyone can help me solving this issue it would mean a lot and thank you for reading!

Localization:
image

Localscript:

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Root = Character:WaitForChild("HumanoidRootPart")
local Head = Character:WaitForChild("Head")

local TweenService = game:GetService("TweenService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local RemoteEvent = script.Parent:WaitForChild("RemoteEvent")
local cooldown = .3
local Order = 1
local Debounce = true


script.Parent.Equipped:Connect(function()
	RemoteEvent:FireServer("Equiped")
end)

script.Parent.Unequipped:Connect(function()
	RemoteEvent:FireServer("Deequipped")
end)

script.Parent.Activated:Connect(function()
	RemoteEvent:FireServer("Attack")
	print("ATTACK!")
end)

Serverscript:

local Remote = script.Parent:WaitForChild("RemoteEvent")
local Handle = script.Parent:WaitForChild("Handle")
local Damage = 10
local Debris = game:GetService("Debris")

local ReplicatedStorage = game:GetService("ReplicatedStorage")

Remote.OnServerEvent:Connect(function(Player, Event, FinalHit)
	local Character = Player.Character or Player.CharacterAdded:Wait()
	local Humanoid = Character:WaitForChild("Humanoid")
	local Root = Character:WaitForChild("HumanoidRootPart")
	
	if Event == "Attack" then
		local Hitbox = Instance.new("Part")
		Hitbox.Parent = workspace
		Hitbox.Anchored = true
		Hitbox.CFrame = Root.CFrame
		Hitbox.CanCollide = false
		Hitbox.Transparency = 0.9
		Hitbox.Size = Vector3.new(8,8,8)
		Debris:AddItem(Hitbox, 4)
		Hitbox.Touched:Connect(function(part)
			print(part:IsDescendantOf(Character)) -- testing to see what the issue is
			print(part) -- testing to see what the issue is
			if not part and not part:IsDescendantOf(Character) then
				local humanoid = part.Parent:FindFirstChildOfClass("Humanoid")	
				humanoid:TakeDamage(10) -- Deal 10 damage to the player
				task.wait(1) -- Wait for 1 second before destroying the hitbox
				Hitbox:Destroy()
			end

		end)
	end
end)

Change to Help and Feedback > Scripting Support.

The server knows when the tool is un/equipped and activated, so a remote is redundant here. You can still use this events on the localscript if you want a client-sided effect. Otherwise, you should remove those connections.

This will cause memory leaks, you should never connect to events in events when they won’t be disconnected. If an exploiter spams the remote, the server will crash without a doubt. Find a way to move this connection outside of the remote connection.

Use game.Debris:AddItem(Hitbox, 1). But I do like how you’re using task.wait, so good for you.

You should instead return when the character is nil, rather than waiting for a new character. This again has the potential for exploiters to crash the server.

When making new instances on the server like this, you should set the Parent property last. Changes only replicate to the client if the parent is not nil, so setting it last will send all the new information in one message, instead of one for each property change.

I recommend adding a debounce or security check to this remote. You wouldn’t want this event to make a part thousands of times per second! Keep in mind, that the debounce should be different for each player, instead of a global debounce. The debounce can also be in the form of a BoolValue, named according to the attack, such as “Slashing”, “Attacking”, etc. This should mean, the remote cannot be called if your already attacking.

Server scripts don’t need to use WaitForChild to wait for things to load.

I appreciate all the tips you gave me and I have changed to #help-and-feedback:scripting-support.

However I could not find within your tips the solution to my issue that I currently have.

You never know, maybe perfecting the current script could fix some things! I did get distracted by the script though. I forgor

I do not blame you. It’s my first time using RemoteEvents within a tool. However the whole unnequip and equip event is necessary for later functionality that I wish to script.

Example: something special happen when equipping the tool.

I will try to use some of your advice however that given above but I do not think this will solve the issue with the hitbox I’m facing. If you desire the weapon itself I wouldn’t mind giving you it so you can test within roblox studio.

Could you maybe explain this as I already included local to handle this?
See example below.

Debris = game:GetService(“Debris”)

1 Like

You’re checking if the part doesn’t exist, rather than checking if it does. This means that it won’t deal the damage if the part does exist.

1 Like

Thank you! I just looked back at the script and saw the script issue ye!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.