Jump to content

[CODING HELP] Target all nearby players


Recommended Posts

Hello!

I'm still learning how to code and I wanted to make a few custom items.

I was in the process of making a book that when read, will heal all nearby players.

 

But I was wondering, how to find all nearby players and if differentiating players from other entities is possible.

I also have some other minor questions. 

 

Like, what exactly is inst?

What does the block 'if not TheWorld.ismastersim then' do

and finally what does inst.entity:SetPristine() do.

 

I couldn't find the above code in the data folders.

 

Thank you for the help!

 

local assets=
{
    --Asset("ANIM", "anim/bookofhealing.zip"),
    --Asset("ANIM", "anim/swap_bookofhealing.zip"),
 
    Asset("ATLAS", "images/inventoryimages/bookofhealing.xml"),
    Asset("IMAGE", "images/inventoryimages/bookofhealing.tex"),
}
prefabs = 
{
}
local function fn()
local function readhealfn(inst, reader)
reader.components.sanity:DoDelta(-TUNING.SANITY_LARGE)
 
local pt = Vector3(reader.Transform:GetWorldPosition())
 
local range = 50
 
local ents = TheSim:FindEntities(pt.x, pt.y, pt.z, range)
 
for k, v in pairs(ents) do
--something something not sure how to target palyer
end
end
 
return true
end
 
 
    local inst = CreateEntity()
 
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
 
--not sure what this does
    inst.entity:AddNetwork()
 
    MakeInventoryPhysics(inst)
 
    --inst.AnimState:SetBank("books")
    --inst.AnimState:SetBuild("books")
    --inst.AnimState:PlayAnimation(name)
 
--not sure what this does
if not TheWorld.ismastersim then
        return inst
    end
inst.entity:SetPristine()
 
inst:AddComponent("inspectable")
inst:AddComponent("book")
 
inst:AddComponent("inventoryitem")
inst.components.inventoryitem.imagename = "bookofhealing"
    inst.components.inventoryitem.atlasname = "images/inventoryimages/bookofhealing.xml"
 
inst.components.book.onread = readhealfn
 
inst:AddComponent("finiteuses")
inst.components.finiteuses:SetMaxUses(3)
inst.components.finiteuses:SetUses(3)
inst.components.finiteuses:SetOnFinished(inst.Remove)
 
 
 
    return inst
end
return  Prefab("common/inventory/bookofhealing", fn, assets, prefabs)
 

 

 

 
Link to comment
Share on other sites

FindEntities can filter by tags.

The entities must have all the tags in must_tags to be found.

The entities must not have any tags in cant_tags to be found.

The entities must have at least one tag of must_oneoftags to be found.

TheSim:FindEntities(pt.x, pt.y, pt.z, range, must_tags, cant_tags, must_oneoftags)

Players have the tag, "player", so you put in must_tags {"player"}, like:

TheSim:FindEntities(inst:GetPosition(), 40, {"player"}, {"playerghost"})

Then you loop through ents.

 

You can also loop through AllPlayers, like:

for _, v in pairs(AllPlayers) do   if inst:GetDistanceSqToInst(v) < 40 then      Heal() --- v.components.health:DoDelta(10)   endend
Edited by DarkXero
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...