Best way to track who killed a npc?

There are alot of video’s and tutorials on this topic, but most of them are poorly made and very exploitable, so i was wondering whats the best way to track who killed a npc and give them a reward such as coins?

2 Likes

I feel obligated to say… why not a table? All you need on the server is a map of NPC to the list of players who’ve done damage to it. Value objects are full Instances, which are comparatively expensive to create, parent to the datamodel, get references to (which is always a property check with dot syntax, followed by a linear search for the matching child, even though it looks like an O(1) reference), set the value of (value objects fire property change events), and you usually end up putting the values in an array anyways to sort them by damage amount, time, or whatever matters to your game. And you have to periodically garbage collect them (yuck!). So why not just have the server keep one table that keys npc id to a queue of recent attackers?

1 Like

Tagging implies putting a data object, “tag” on the NPC itself. Using a map is the polar opposite from tagging, it’s centrally storing the data.

I’m not nitpicking terminology, these are entirely different solutions to the same problem. Very different architectures. I wasn’t making any assumptions about what system you were suggesting, this:

Is pretty unambiguous.

EDIT: Just to be clear, I’m not saying what you suggest is bad, or even something you should not do. Performance wise, it’s unlikely to matter. But it is a very cumbersome system to set up, compared to just a simple key value map. I just want the OP to know all the options.

It’s not off-topic at all. The topic is about the best way to do a specific task, the one we’re discussing. You asserted that you think tagging the NPCs is “the most effective and potentially the only way” to solve this problem. I simply don’t agree, and gave my case for the system I would use. It’s not personal, I just think the asker of this question deserves more than one option.

2 Likes

If you believed it deserved more than one answer then couldn’t you of just replied to the OP?
Kinda derails the thread and doesn’t give any extra information relating to the subject.

1 Like

Because it’s not just about providing another option; using ValueObjects in lieu of lightweight and simple data structures is a poor practice that is often recommended here, and it should be challenged whenever it is. Using value objects in this way doesn’t scale well to other tasks or translate to other languages and development environments. But when new scripters are told this is a good way to do things, they go on and try to use this all over the place. I’ve seen examples of Roblox games that unpack large amounts of player data into hierarchical structures of value objects! And then the devs wonder why their game has a big lag spike whenever a player joins or leaves… :roll_eyes: We should try to teach new scripters good practices.

I disagree. I would contend that offering a better-performing solution to the problem posed by the original poster, and explaining in detail why it should be considered, does not constitute “derailing the thread” without any “extra information”. These forums are for technical discussion, which is necessarily going to involve debating of techniques, and corrections to erroneous assertions such as tagging and keeping a central lookup table being variants of the same thing–they simply are not. It’s not being argumentative to refute such a statement, I would say it’s obligatory to clarify it, since the purpose of this forum is to help scripters improve their techniques and make better-performing games, and passing on age-old bad practices is counter to this goal.

5 Likes

I can agree with what you first stated, that wasn’t my argument. All I’m saying is that if you had replied to this thread with a different solution it could of stopped any form of issue building. I’m happy to go into private messages about this if there’s anything else that needs to be addressed.

Please look at my first reply to your solution, it was a well-intentioned suggestion to consider the use of a table. My next reply is just a straightforward correction regarding tagging. I’d like to politely ask you to just take a moment to consider why you are taking this so personally, because the tone of telling me to “give it a rest” is not very nice. :neutral_face:

And as for giving it a rest, alas, this journey to understanding is not yet at an end, because:

Not supposedly different, different. Fundamentally different solutions at the implementation level, and that’s the level where the important distinction lives. You are correct that keeping a table and tagging an object both make associations between NPC and attackers, but things like where and how you store your data, and what direction associations are stored in, matter critically in computer science. These are the kinds of decisions that can make or break the performance of a system as it scales, and it’s why I’m belaboring this point. This isn’t just about NPCs, it’s about software engineering concepts and how there can be big performance differences between solutions that seem superficially similar.

Tagging an object means that you store some value on the object; it need not be a value object specifically, it could be some other type of child or a property of the object. The defining property of a tag is that it lives with the object and its lifespan is that of the object. When you use a table instead, you’re not modifying the object in any way, and both your reference to the NPC and the data live on even after the NPC is destroyed. The ramifications of this type of design choice can be huge in terms of run-time cost to access and modify the data.

2 Likes

Yes, because this is a forum where game developers come to learn about programming. These concepts are exactly what they need to learn to write better code. I don’t know the experience level of the OP, but to me “associating the player to the npcs” is restatement of their problem. Maybe our interpretation differs, but for me track means store and update this association and best way means best way to implement.

And I see this is a debate about teaching good practices, not a confrontation. I’m not taking this personally, just trying get my point about the difference of these two solutions understood. My ELI5 version would look like this:

Problem: You need to remember all your coworkers’ phone numbers
Tagging solution: You write each person’s number on a post-it note and stick in on their back.
Table solution: You put each person’s number in your phone’s contact list.

This I think is a version that makes the huge difference in these approaches accessible to even a five year old. It’s also a good illustration of how these 2 different ways of associating data work for different problems: obviously putting stickers on people is not a good solution for phone numbers, but it’s the go-to solution for associating names to people at a party.

3 Likes

Use a script like this:
thing.Touched:Connect(function(hit) – Calls that part touched
local plr = game.Players:GetPlayerFromCharacter(hit.Parent) – gets the player who touched
end)
Hopefully this helps
ExoticTwix

You could track if an NPC has 0 or less health after making it take damage, then fire a BindableEvent (or RemoteEvent, depends on how you’re doing it) to the server to award a player coins (the BindableEvent isn’t needed if you can get the playerstats from the damage script).

1 Like