Jump to content

Some codes for custom sword


Recommended Posts

Here you go I wasn't sure if you wanted to restore health from kill or hit. As for health 5% i'd let you customize the heal as i don't know the weapon damage.

Code:

local function onattack(inst, owner, target) ---restores health on kill
    if owner.components.health and owner.components.health:GetPercent() < 1 and not target:HasTag("wall") then
        owner.components.health:DoDelta(8) ---heal 8, change it to your number
    end
end

local function OnHit(inst, owner, target) ----restores health on hit
    if owner.components.health and owner.components.health:GetPercent() < 1 and not target:HasTag("wall") then
        owner.components.health:DoDelta(8) ---heal 8, change it to your number
    end
end

local function fn(colour) ---the main function of your sword

if not TheWorld.ismastersim then
   return inst
end

inst.components.weapon.attackrange = 8 ----how far your weapon can hit
    inst.components.weapon.hitrange = 8 ----how far your hits can land
    inst.components.weapon.onattack = onattack ---call the function for healing on kill
    inst.components.weapon.OnHit = OnHit ---call the function for healing on hit

end

Link to comment
Share on other sites

14 hours ago, K1NGT1GER609 said:

Here you go I wasn't sure if you wanted to restore health from kill or hit. As for health 5% i'd let you customize the heal as i don't know the weapon damage.

Well when i said Restores health %5 of damage i mean when i hit the enemy the sword will restore health 5% of damage

example

 

My sword has 40 dmg on based (but it'll change sometimes) and it must restore 2 health per hit.

also thanks for help.

 

Link to comment
Share on other sites

I tried to add this code but the game crash here is the crash log and code

in master_postinit

	inst:ListenForEvent("equip", function()
	    local equippedweb = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		
		if equippedweb ~= nil and equippedweb:HasTag("fuingetsu") then
		
		inst.components.attackrange = 8
		inst.components.hitrange = 8
	
		 elseif inst.level <461 then
			inst.components.weapon.combat.damagemultiplier = 1.5
			
		else
			inst.components.weapon.combat.damagemultiplier = 0.7
			
		end
	end)
	
	inst:ListenForEvent("unequip", function()
        local unequippedwep = inst.components.inventory:GetEquippedItem(EQUIPSLOTS.HANDS)
		
		if unequippedwep ~= nil and unequippedwep:HasTag("fuingetsu") then
			inst.components.combat.damagemultiplier = 1
		else
			inst.components.combat.damagemultiplier = 1
				
		end
	end)

and here is log

[00:00:47]: [string "../mods/alishia/scripts/prefabs/alishia.lua"]:771: attempt to index field 'weapon' (a nil value)
LUA ERROR stack traceback:
    ../mods/alishia/scripts/prefabs/alishia.lua:771 in (local) fn (Lua) <766-781>
    scripts/entityscript.lua:998 in (method) PushEvent (Lua) <985-1012>
    scripts/components/inventory.lua:895 in (method) Equip (Lua) <804-901>
    scripts/components/inventory.lua:177 in (method) OnLoad (Lua) <159-191>
    scripts/entityscript.lua:1525 in (method) SetPersistData (Lua) <1516-1533>
    scripts/mainfunctions.lua:1323 in () ? (Lua) <1319-1331>
    =[C]:-1 in (method) SendResumeRequestToServer (C) <-1--1>
    scripts/prefabs/world_network.lua:30 in (field) fn (Lua) <19-34>
    scripts/scheduler.lua:177 in (method) OnTick (Lua) <155-207>
    scripts/scheduler.lua:371 in (global) RunScheduler (Lua) <369-377>
    scripts/update.lua:170 in () ? (Lua) <149-228>

 

Link to comment
Share on other sites

1 minute ago, Lumina said:

Why are you adding this to the character instead of the sword ? I don't understand.

Well i don't have any idea :/ İ just thought that i must add them to character, i'm new at coding custom item.

Link to comment
Share on other sites

I guess you can add it to character, but it seems pretty much not really the thing to do if what you want is add something to the weapon.

Adding it to the weapon allows every character using the weapon to benefit the special effect. If you add this to the character, only the character will benefit it. Which could be what you want, but since you didn't mention it, it doesn't seems to be the case.

Try using the code provided by K1NGT1GER609, see if it does what you want, and if not, come here explaining what is wrong, would be a lot more easy i think.

Link to comment
Share on other sites

1 minute ago, Lumina said:

I guess you can add it to character, but it seems pretty much not really the thing to do if what you want is add something to the weapon.

Adding it to the weapon allows every character using the weapon to benefit the special effect. If you add this to the character, only the character will benefit it. Which could be what you want, but since you didn't mention it, it doesn't seems to be the case.

Try using the code provided by K1NGT1GER609, see if it does what you want, and if not, come here explaining what is wrong, would be a lot more easy i think.

Well it seems like the first code is quite a bit different than i wanted. I mean main part is fine but it seems like every time restores same vaule but it must be proportional with damage. How i can change it?

local function OnHit(inst, owner, target) ----restores health on hit
    if owner.components.health and owner.components.health:GetPercent() < 1 and not target:HasTag("wall") then --I DONT KNOW WHAT IS THIS 
        owner.components.health:DoDelta(8) ---THIS PART MUST BE 5% OF DAMAGE
    end
end

also can i add this code into mysword.lua?

Link to comment
Share on other sites

You must add this code into mysword.lua, yes.

And don't forget to add the other part too, or it will not work.

 


local function fn(colour) ---the main function of your sword

if not TheWorld.ismastersim then
   return inst
end

inst.components.weapon.attackrange = 8 ----how far your weapon can hit
    inst.components.weapon.hitrange = 8 ----how far your hits can land
    inst.components.weapon.OnHit = OnHit ---call the function for healing on hit

end 

Note : you must ADD this part :

 


inst.components.weapon.attackrange = 8 ----how far your weapon can hit
    inst.components.weapon.hitrange = 8 ----how far your hits can land
    inst.components.weapon.OnHit = OnHit ---call the function for healing on hit

The other lines are supposed to already exist, they are here to show you where to add the lines. You should already have them.

 

Also, i'm sure it's possible to change a fixed value to a value depending of your damage, i'm not able to do it myself but it's possible. But if you make the code work, changing the value is still possible after :)

Link to comment
Share on other sites

Sorry to bump this thread, but since this exists I'd rather not make a new one. I'm having issues getting the return health on-hit code to work for my weapon. I just want a simple lifesteal of like 7hp per hit, sorta like the batbat but stronger. The code doesn't crash them game, or cause any issues, it just doesn't seem to do anything. I am probably putting it in the wrong place, so I attached the weapon .lua file so someone can correct me. Thanks in advance!

shadowrapier.lua

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...