• Draw text when player enter zone
    9 replies, posted
Hello! How I can do when player enter zone then show text and then player leave zone text hide? I try this function DrawInfo(pos, txt, clr)     pos = pos:ToScreen()     draw.TextShadow( {         text = txt,         pos = { pos.x, pos.y },         font = "FontName",         color = clr,         xalign = TEXT_ALIGN_CENTER,         yalign = TEXT_ALIGN_CENTER,     }, 2, 255 ) end MY_ZONE = Vector( 123, 123, 123 ) if LocalPlayer():Team() == TEAM_CITIZEN then for k,v in pairs(ents.FindInSphere(MY_ZONE, 100)) do DrawInfo(MY_ZONE, "Warning!", Color(255, 0, 0)) end end
You need to add if statement inside for loop. If the entity is a localplayer then show text
It needs to be inside a 2d hook. If you want to draw the warning only if it's a specific entity use GetClass. (dont only check for local players, your weapons are entities too) ALSO break the loop otherwise it will be drawn x the amount of entities found.
GM/HUDPaint Knowing that you are searching for the player and checking if they are inside, then drawing ... Put this inside hudpaint ON CLIENT: MY_ZONE = Vector( 123, 123, 123 ) if LocalPlayer():Team() == TEAM_CITIZEN then for k,v in pairs(ents.FindInSphere(MY_ZONE, 100)) do if v == LocalPlayer() then DrawInfo(MY_ZONE, "Warning!", Color(255, 0, 0)) end end end inside of your HUDPaint.
More efficient way is just grabbing your own position and using Vector/WithinAABox, instead of looping over the entities in that sphere for no good reason what-so-ever
Better way would be using triggers as you can setup outputs to fire when player enters or leaves it.
Honestly I think this is the easiest managed way, especially give StartTouch and EndTouch are functions you can use which are typically what you would need for something like this
if u want do it that way, then your code should look like this MY_ZONE = Vector( 123, 123, 123 ) local function DrawWarningText() local plyF = ents.FindInSphere( MY_ZONE, 100 ) for k, v in pairs(plyF) do if LocalPlayer():Team() == TEAM_CITIZEN then local pos = MY_ZONE:ToScreen()     draw.TextShadow( {         text = "Warning!",         pos = { pos.x, pos.y },         font = "FontName",         color = Color(255, 0, 0),         xalign = TEXT_ALIGN_CENTER,         yalign = TEXT_ALIGN_CENTER,     }, 2, 255 ) end end end hook.Add("DrawOverlay", "DrawWarnings", DrawWarningText)
I really really hope you're not writing code like that, since i had to fix someone's code that did the same shit that for some reason decided to add one extra line for every line making the code readability imposible
It looks awful in code tags. I call it kinda enterprise code. But I agree that sometimes its not lookin' good. Usually I skip some tabs, some not.
Sorry, you need to Log In to post a reply to this thread.