Jump to content

Function "canattack" (need help)


Recommended Posts

Hello everyone!
I have this code:

local function onequip_friendship(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "torso_amulets_custom", "greenamulet")

    if owner.components.combat ~= nil then
        owner.components.combat.canattack = false
    end
end

local function onunequip_friendship(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")

    if owner.components.combat ~= nil then
        owner.components.combat.canattack = true
    end
end

This prohibits the item owner from attacking people and mobs.
I want that a player not attack other players, but he could still attack mobs.
Is it possible to implement?
If this is too difficult, please tell me about it so that I do not expect help.
Thank you for attention.

Edited by Tezumoto
Link to comment
Share on other sites

This might be somewhat tricky to implement, It depends on what item this is you are equiping (is that an amulet? or a weapon? or a character trait?)

 if owner.components.combat ~= nil then

This essentially means "if you have the ability to attack," which you always do, Essentially making it impossible to attack.

The easiest way to tell if an enemy is another player is (object):HasTag("player")

This might be something you can use

if owner.components.combat.target:HasTag("player") then
	owner.components.combat.canattack = false
else
	owner.components.combat.canattack = true --(THOUGH THIS MIGHT CAUSE PROBLEMS, NOT SURE)
end

However, you would need to find a way to run this right before each attack you make, before the actual swing comes out. Otherwise, it will never update who their target is.

Link to comment
Share on other sites

3 hours ago, pickleplayer said:

This might be something you can use


if owner.components.combat.target:HasTag("player") then
	owner.components.combat.canattack = false
else
	owner.components.combat.canattack = true --(THOUGH THIS MIGHT CAUSE PROBLEMS, NOT SURE)
end

However, you would need to find a way to run this right before each attack you make, before the actual swing comes out. Otherwise, it will never update who their target is.

I have already tried to use this code and many of its variations.
But unfortunately did not find a working option.

Link to comment
Share on other sites

--modmain.lua
AddComponentPostInit("combat", function(self)
	self.inst.replica.combat.oldCanBeAttacked = self.inst.replica.combat.CanBeAttacked
	function self.inst.replica.combat:CanBeAttacked(attacker)
		local oldReturn = self.inst.replica.combat:oldCanBeAttacked(attacker)
		if self.inst:HasTag("player") and oldReturn then
			return not attacker:HasTag("friendship")
		end
		return oldReturn
	end
end)

--youritem.lua
local function onequip_friendship(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "torso_amulets_custom", "greenamulet")

  	owner:AddTag("friendship")
end

local function onunequip_friendship(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")

	owner:RemoveTag("friendship")
end

 

Link to comment
Share on other sites

12 hours ago, Aquaterion said:

--modmain.lua
AddComponentPostInit("combat", function(self)
	self.inst.replica.combat.oldCanBeAttacked = self.inst.replica.combat.CanBeAttacked
	function self.inst.replica.combat:CanBeAttacked(attacker)
		local oldReturn = self.inst.replica.combat:oldCanBeAttacked(attacker)
		if self.inst:HasTag("player") and oldReturn then
			return not attacker:HasTag("friendship")
		end
		return oldReturn
	end
end)

--youritem.lua
local function onequip_friendship(inst, owner)
    owner.AnimState:OverrideSymbol("swap_body", "torso_amulets_custom", "greenamulet")

  	owner:AddTag("friendship")
end

local function onunequip_friendship(inst, owner)
    owner.AnimState:ClearOverrideSymbol("swap_body")

	owner:RemoveTag("friendship")
end

 

Oh my god, it works!!!
Dude, you're the best, thank you for your help!

Edited by Tezumoto
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...