Jump to content

Recommended Posts

So I ave been trying to give one of my custom characters the ability to affect other players sanity.
It's not my first time making a character, but I have never made one that influenced other players.

His perk should in the end be:
Other players gain a little bit of sanity at night from being close to him.
At day time their sanity should slightly drain from being too  close to him.

inst.components.sanityaura.aura = -TUNING.SANITYAURA_SMALL

I tried using this from the Evil Flower but it kept crashing my game.

While going through the forums here I found this one:

But the code they used for the health regeneration (also including sanity) didn't work for me as soon as I tried to add the day and night bit.

If anyone has an idea how I could solve my issue I'd be really grateful! Every little bit help, maybe I could help you with some art things in return.
 

I think this would work, tell me if it doesn't or it crashes!

 

outside master_postinit of YOURCHARACTER.lua

Spoiler

local function custom_sanity_aura(inst, phase)
local x, y, z = inst.Transform:GetWorldPosition()
local players = FindPlayersInRange(x, y, z, (8), true) -- "8" = Distance between you & others.

    if phase == "day" then
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    if inst.sanity_aura_night == true then -- If any other sanity auras are in effect cancel them.
    inst.sanity_aura_night = nil
    inst.sanity_aura_night:Cancel()
    elseif sanity_aura_dusk == true then
    inst.sanity_aura_dusk = nil
    inst.sanity_aura_dusk:Cancel()
    end

    inst.sanity_aura_morning = true
    inst.sanity_aura_morning = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(-.5)
    end
    end
    end)

    elseif phase == "night" then
    if inst:HasTag("playerghost") then
    return
    end

    if inst.sanity_aura_morning == true then -- If any other sanity auras are in effect cancel them.
    inst.sanity_aura_morning = nil
    inst.sanity_aura_morning:Cancel()
    elseif sanity_aura_dusk == true then
    inst.sanity_aura_dusk = nil
    inst.sanity_aura_dusk:Cancel()
    end

    inst.sanity_aura_night = true
    inst.sanity_aura_night = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(.5)
    end
    end
    end)


    elseif phase == "dusk" then
    if inst:HasTag("playerghost") then
    return
    end

    if inst.sanity_aura_morning == true then -- If any other sanity auras are in effect cancel them.
    inst.sanity_aura_morning = nil
    inst.sanity_aura_morning:Cancel()
    elseif sanity_aura_night == true then
    inst.sanity_aura_night = nil
    inst.sanity_aura_night:Cancel()
    end

    inst.sanity_aura_dusk = true
    inst.sanity_aura_dusk = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(.1)
    end
    end
    end)
end
end

inside master_postinit of YOURCHARACTER.lua

inst:WatchWorldState("phase", custom_sanity_aura)

Hopefully this works for you :D!

 

@Leothenewguy You'd also add this into YOURCHARACTER.lua is master_postinit

Spoiler

inst:DoTaskInTime(1, function()
 if TheWorld.state.isnight then -- Login while night.
     if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if ded.
	 
	inst.sanity_aura_night = true
    inst.sanity_aura_night = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(.5)
    end
    end
    end)
	
  elseif TheWorld.state.isdusk then -- Login while dusk.
     if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if ded.
	 
	 inst.sanity_aura_dusk = true
    inst.sanity_aura_dusk = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(.1)
    end
    end
    end)
	 
elseif TheWorld.state.isday then -- Login while day.
     if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if ded.
	 
	 inst.sanity_aura_morning = true
    inst.sanity_aura_morning = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(-.5)
    end
    end
    end)
	 
  end
end)

 

 

Edited by SuperDavid

Hey @SuperDavid! Thank you so much for the help.
The only issue is as soon as I add the third code the game will let me get to the point that I spawn and then I get an error that "player" is not defined.
Without the third one it runs so I am certain that it has to do with that one. It was correct to add it underneath the master_postinit so it looks like this

local master_postinit = function(inst)

inst:DoTaskInTime(1, function()
 if TheWorld.state.isnight then -- Login while night.
     if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if ded.
	 
	inst.sanity_aura_night = true
    inst.sanity_aura_night = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(.5)
    end
    end
    end)
	
  elseif TheWorld.state.isdusk then -- Login while dusk.
     if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if ded.
	 
	 inst.sanity_aura_dusk = true
    inst.sanity_aura_dusk = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(.1)
    end
    end
    end)
	 
elseif TheWorld.state.isday then -- Login while day.
     if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if ded.
	 
	 inst.sanity_aura_morning = true
    inst.sanity_aura_morning = inst:DoPeriodicTask(1, function()
    if inst:HasTag("playerghost") or inst.components.health:IsDead() then return end -- Don't do anything if dead.

    for _, v in pairs(players) do
    if (v~=inst) and (v.components and v.components.sanity) and v.components.health and not v.components.health:IsDead() then
    v.components.sanity:DoDelta(-.5)
    end
    end
    end)
	 
  end
end)

and then continues on with the stats and so on. Do you maybe know how to fix it? 

Edited by Leothenewguy
Just now, Leothenewguy said:

Sure if you could tell me how to do that I am actively using this page for the first time :D
Should I just copy the entire prefab code or can I somehow upload the Mod file?

 

Create a zip folder on your desktop then copy your entire mod folder into the zip then attach the file in a post here :D!!!

There you go sorry for the hold up my Pc decided to take ages to zip it.
It 's still at the beginning so no own art yet :D
Thanks again for bothering with me :3 
Edit: Oh and I kept your codes in

Jack.rar

Edited by Leothenewguy

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
×
  • Create New...