# Gain sanity near other players?

## Recommended Posts

Hello! I was trying to work on a perk for my character that would allow her to gain sanity when she's near other players... However, I can't seem to find a way to distinguish other players from my modded character.  Code is in the spoiler:

Spoiler

local function sanityfn(inst)        local x,y,z = inst.Transform:GetWorldPosition()            local delta = 0        local ents = TheSim:FindEntities(x,y,z, 20, {"player"})        for k,v in pairs(ents) do             local bonus_sanity = TUNING.SANITYAURA_LARGE            local distsq = math.max(inst:GetDistanceSqToInst(v),1)            delta = delta + bonus_sanity/distsq        end        return delta    end        inst.components.sanity.custom_rate_fn = sanityfn

So basically my problem is that she is gaining sanity near others BUT it's also counting herself as an 'other' and that's not what I want. I want her to gain sanity only when she's near someone else rather than it counting herself as a sanity gain.

Edited by Nipi
##### Share on other sites

```local function sanityfn(inst)
local x, y, z = inst.Transform:GetWorldPosition()
local delta = 0
local ents = TheSim:FindEntities(x, y, z, 20, {"player"})
for k, v in pairs(ents) do
if v ~= inst then
local bonus_sanity = TUNING.SANITYAURA_LARGE
local distsq = math.max(inst:GetDistanceSqToInst(v), 1)
delta = delta + bonus_sanity / distsq
end
end
return delta
end

-- master_postinit
inst.components.sanity.custom_rate_fn = sanityfn
```

##### Share on other sites

@DarkXero It worked thank you so much!

##### Share on other sites

On 11.05.2017 at 2:22 AM, DarkXero said:
```
local function sanityfn(inst)
local x, y, z = inst.Transform:GetWorldPosition()
local delta = 0
local ents = TheSim:FindEntities(x, y, z, 20, {"player"})
for k, v in pairs(ents) do
if v ~= inst then
local bonus_sanity = TUNING.SANITYAURA_LARGE
local distsq = math.max(inst:GetDistanceSqToInst(v), 1)
delta = delta + bonus_sanity / distsq
end
end
return delta
end

-- master_postinit
inst.components.sanity.custom_rate_fn = sanityfn
```

is it possible to make it fo other players? I mean when they near you they gain sanity or walk and attack speed? Is it possible to make a code like this?

##### Share on other sites

Yes, this is possible. For players gaining sanity near you, you have to give your character a sanity aura.

```

inst.components.sanityaura.aura = -TUNING.SANITYAURA_LARGE -- Instead of LARGE you can use, TINY, SMALL, MED, or HUGE. Removing the - will make players gain sanity from your character instead. ```

Something like this i think. Never tested it myself.

##### Share on other sites

52 minutes ago, AkaiNight said:

is it possible to make it fo other players? I mean when they near you they gain sanity or walk

```local AURA_RADIUS = 15
local SANITY_BONUS = GLOBAL.TUNING.DAPPERNESS_MED
local SPEED_MULTIPLIER = 1.25
local COMBAT_MULTIPLIER = 1.25
local AURA_NAME = "akaimodaura"

local function UpdateAuras(inst)
for i, v in ipairs(GLOBAL.AllPlayers) do
if v ~= inst then
local prev = v[AURA_NAME] or false
if prev ~= activate then
if activate then
v.components.sanity.dapperness = v.components.sanity.dapperness + SANITY_BONUS
v.components.locomotor:SetExternalSpeedMultiplier(inst, "superspeed", SPEED_MULTIPLIER)
v.components.combat.damagemultiplier = (v.components.combat.damagemultiplier or 1) * COMBAT_MULTIPLIER
else
v.components.sanity.dapperness = v.components.sanity.dapperness - SANITY_BONUS
v.components.locomotor:RemoveExternalSpeedMultiplier(inst, "superspeed")
v.components.combat.damagemultiplier = v.components.combat.damagemultiplier / COMBAT_MULTIPLIER
end
end
v[AURA_NAME] = activate
end
end
end

if GLOBAL.TheWorld.ismastersim then
end
end)
```

Here's a convenient package. Sanity, walk speed, and damage aura.

You put it in modmain and you change "wilson" to the prefab your character is.

53 minutes ago, AkaiNight said:

attack speed

Forget it.

##### Share on other sites

14 hours ago, DarkXero said:
```
local SANITY_BONUS = GLOBAL.TUNING.DAPPERNESS_MED
local SPEED_MULTIPLIER = 1.25
local COMBAT_MULTIPLIER = 1.25
local AURA_NAME = "akaimodaura"

local function UpdateAuras(inst)
for i, v in ipairs(GLOBAL.AllPlayers) do
if v ~= inst then
local prev = v[AURA_NAME] or false
if prev ~= activate then
if activate then
v.components.sanity.dapperness = v.components.sanity.dapperness + SANITY_BONUS
v.components.locomotor:SetExternalSpeedMultiplier(inst, "superspeed", SPEED_MULTIPLIER)
v.components.combat.damagemultiplier = (v.components.combat.damagemultiplier or 1) * COMBAT_MULTIPLIER
else
v.components.sanity.dapperness = v.components.sanity.dapperness - SANITY_BONUS
v.components.locomotor:RemoveExternalSpeedMultiplier(inst, "superspeed")
v.components.combat.damagemultiplier = v.components.combat.damagemultiplier / COMBAT_MULTIPLIER
end
end
v[AURA_NAME] = activate
end
end
end

if GLOBAL.TheWorld.ismastersim then
end
end)
```

Here's a convenient package. Sanity, walk speed, and damage aura.

You put it in modmain and you change "wilson" to the prefab your character is.

Forget it.

thanks a lot for code

##### Share on other sites

15 hours ago, Lumina said:

Yes, this is possible. For players gaining sanity near you, you have to give your character a sanity aura.

```

inst.components.sanityaura.aura = -TUNING.SANITYAURA_LARGE -- Instead of LARGE you can use, TINY, SMALL, MED, or HUGE. Removing the - will make players gain sanity from your character instead. ```

Something like this i think. Never tested it myself.

thanks a lot

##### Share on other sites

On 11.05.2017 at 2:22 AM, DarkXero said:
```
local function sanityfn(inst)
local x, y, z = inst.Transform:GetWorldPosition()
local delta = 0
local ents = TheSim:FindEntities(x, y, z, 20, {"player"})
for k, v in pairs(ents) do
if v ~= inst then
local bonus_sanity = TUNING.SANITYAURA_LARGE
local distsq = math.max(inst:GetDistanceSqToInst(v), 1)
delta = delta + bonus_sanity / distsq
end
end
return delta
end

-- master_postinit
inst.components.sanity.custom_rate_fn = sanityfn
```

for this code if i chance player with  critter_kitten is that work (i mean when kittykit around will i gain sanity?) and how much sanity will i gain per minunte can i edit this like 5 sanity per min? Or can i make it lose sanity like 7 per min?

Edited by AkaiNight
##### Share on other sites

I don't know for the critter_kitten part, but you could change value of sanity, just change

`TUNING.SANITYAURA_LARGE`

By something else like the one suggested in my post. Do

`-TUNING.SANITYAURA_LARGE`

for negative aura

##### Share on other sites

1 minute ago, Lumina said:

I don't know for the critter_kitten part, but you could change value of sanity, just change

```
TUNING.SANITYAURA_LARGE```

By something else like the one suggested in my post. Do

```
-TUNING.SANITYAURA_LARGE```

for negative aura

thanks agai