Trenix Posted May 29, 2020 Share Posted May 29, 2020 So because I'm working on a PvP mod, I need to start dealing with buffs. I used the buff API and I think I set it correctly for the most part, yet the game crashes. My biggest problem is how listen events work. It seems like you have to create a function to begin to listen, but to run the function, it has to be called by another function. That is why I tried using AddPlayerPostInit, yet whenever I tried to do any of this, it ALWAYS crashes. I don't know what I'm doing wrong. Please help. ---- [Slow Players Upon PvP Attack] ---- local function Applybuff(buffData, instToBuff) local buffUniqueName = buffData.uniqueName -- Cancel any existing duplicate timed buff. if instToBuff[buffUniqueName.."Task"] ~= nil then buffData.removeFunction(instToBuff) instToBuff[buffUniqueName.."Task"]:Cancel() instToBuff[buffUniqueName.."Task"] = nil end -- Make sure that our buff is removed BEFORE the entity is saved (the server is closed or player quits). -- This is only necessary if you change variables which are saved, like current health, health-penalty, etc. -- Without it, you could apply a max-health buff, quit before it ran out, rejoin, and you would have kept -- the extra max health! local buffOnSave = function(self, inst, data) if inst[buffUniqueName.."Task"] ~= nil then buffData.removeFunction(inst) inst[buffUniqueName.."Task"]:Cancel() inst[buffUniqueName.."Task"] = nil end end if instToBuff.OnSave ~= nil then local oldOnSave = instToBuff.OnSave instToBuff.OnSave = function(self, inst, data) buffOnSave(self, inst, data) oldOnSave(self, inst, data) end else instToBuff.OnSave = buffOnSave end -- Apply the buff. buffData.applyFunction(instToBuff) -- Set up removal after a certain amount of time. instToBuff[buffUniqueName.."Task"] = instToBuff:DoTaskInTime(buffData.duration, function(inst) buffData.removeFunction(inst) inst[buffUniqueName.."Task"] = nil end) end -- Now to make some buffs and put them in a Buffs-dictionary. local Buffs = {} -- This function creates a new buff-data object and adds it to the Buffs-dictionary -- with the uniqueName as key. local function CreateBuff(uniqueName, duration, applyFunction, removeFunction) local newBuff = {} newBuff.uniqueName = uniqueName newBuff.duration = duration newBuff.applyFunction = applyFunction newBuff.removeFunction = removeFunction Buffs[uniqueName] = newBuff end -- This is an example of how to use the CreateBuff() function to create a new buff. CreateBuff("SlowDownPlayer", 3.0, function(inst) inst.components.locomotor:SetExternalSpeedMultiplier(inst, "Slow", 0.1) end, function(inst) inst.components.locomotor:RemoveExternalSpeedMultiplier(inst, "Slow") end) end) AddPlayerPostInit(function(inst) inst.ListenForEvent("attacked", function(inst,data) PVPATTACK(inst, data, false) end) local function PVPATTACK(inst,data) if data.attack==nil or inst==nil then return end if data.attacker:HasTag("player") then print("It works") ApplyBuff(Buffs["SlowDownPlayer"], data.attacker) end end Link to comment https://forums.kleientertainment.com/forums/topic/118583-trying-buffs-help/ Share on other sites More sharing options...
Trenix Posted May 29, 2020 Author Share Posted May 29, 2020 Alright so I figured it out, Applybuff should of been ApplyBuff and inst.ListenForEvent should of been inst:ListenForEvent. The effect works but I'm having trouble applying it to the right person. Seems like everyone always applies stuff to the attacker, but how do you get data on the person who is attacked? Link to comment https://forums.kleientertainment.com/forums/topic/118583-trying-buffs-help/#findComment-1337802 Share on other sites More sharing options...
Trenix Posted May 30, 2020 Author Share Posted May 30, 2020 Nevermind, the inst in the attacked event is the player who was attacked. The data.attacker is obviously the attacker. Here is the code for people who are trying to do something similar. AddPlayerPostInit(function(inst) inst:ListenForEvent("attacked", function(inst, data) if inst==nil or data.attacker==nil then return end if inst:HasTag("player") and data.attacker:HasTag("player") then ApplyBuff(Buffs["SlowDownPlayer"], inst) end end) end) For here, the buff I'm applying only effects PvP interaction. The attack and the player who is attacked, both have to be actual players. Link to comment https://forums.kleientertainment.com/forums/topic/118583-trying-buffs-help/#findComment-1337913 Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now