so basically, i have the basics of my combat system done, i want to make it so that its PVE and not PVP so players can’t attack other players, only NPC’s, how can that be done?
add an attribute to the npc, name it NPC, and wherever you check if its a player, just check if the model has the NPC attribute, if so then continue with the attack
Everytime the player hits something with a humanoid check if its a player by doing this
if not Players:FindFirstChild(Hit.Name) then
-- Not a player
else
-- Is a Player
end
Hit being the character model or any other way of getting the players name like from the humanoid.
Since if you don’t find the player in the players service then they aren’t a player.
that seems like my best bet, yeah i think I’ll do that.
so i have the code, i have an attribute isNPC and its set to true, do you know why it isn’t damaging anymore?
hitbox.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and hit.Parent ~= character then
local isNPC = hit.Parent:GetAttribute("IsNPC")
if isNPC and isNPC == true then
humanoid:TakeDamage(damage)
end
end
end)
hitbox.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and hit.Parent ~= character then
local p = game.Players:GetPlayerFromCharacter(hit.Parent)
if p == nil then
humanoid:TakeDamage(damage)
else
print("This is a player")
end
end
end)
is this way more optimized then using the attributes?
Yes bc using attributes would mean that every npc in the game has to have the attribute added to them and it also uses up more unecessary code
ok i appreciate it, yeah it seems to work now, thanks.
would you be able to recommend a way to ensure the npc is only hit once per “punch/attack” since its a hitbox it hits a few times
hitbox.Touched:Connect(function(hit)
local deb = false
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and hit.Parent ~= character and deb == false then
local p = game.Players:GetPlayerFromCharacter(hit.Parent)
if p == nil then
deb = true
humanoid:TakeDamage(damage)
else
print("This is a player")
end
end
end)
that didn’t work for me, it still took multiple hits of damage.
local deb = false
hitbox.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid and hit.Parent ~= character and deb == false then
local p = game.Players:GetPlayerFromCharacter(hit.Parent)
if p == nil then
deb = true
humanoid:TakeDamage(damage)
task.wait(0.5)
deb = false
else
print("This is a player")
end
end
end)
You can do what I did with my code where I make a “HitHumanoids” table ‘inside’ the hitbox function and it adds the humanoid to the table
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.