What’s the best way to make a death message in chat that will show when a player dies to a zombie, like “Player died to Fast Zombie” for example. I already know how to use textchatservice to make system messages, but what would be the best way of actually detecting which zombie killed the player and also getting it to show for other people?
You can either do it the beginner way (a script in each zombie that fires an event when the player it’s attacking dies), or the cool way (a module script that tracks all the zombies as objects, and those zombie objects call a function to create a chat message when they kill their target)
could you elaborate more on the module, so basically would it be client-sided or server-sided and how would I detect if each zombie kills their target, thanks!
maybe I could store a value inside each zombie that has their target, and then loop through all the zombies and check if their target value is dead? better than firing an event for every zombie
Basically just a module script on the server-side that stores an object class for a zombie, pretty much:
local Zombie = {}
Zombie.__index = Zombie
function Zombie.new(model : Model)
local newZombie = setmetatable({},Zombie)
newZombie.Name = model.Name
newZombie.Model = model
newZombie.Target = nil -- Would be a Player
newZombie.Humanoid = model:FindFirstChild("Humanoid")
return newZombie
end
And then when the server spawns in a new zombie, it goes to this module script to create a new Zombie object to track it with.
When a zombie finally has a Target set, you’ll want to track when the zombie attacks. Each time it attacks, check the Target’s health. If it’s below zero (dead), you then call the function that creates the death message, preferably taking in a parameter for the target’s name and zombie’s name, so you can do:
if zombie.Target.Character.Humanoid.Health <= 0 then
createDeathMessage(zombie.Target.Name, zombie.Name)
end
alright, thanks! also kinda unrelated but if I use this OOP system could I also use inheritance? like lets say if I want to make a new zombie that explodes upon death but also attacks player, I could just inherit an attack method that was already in my normal zombie class for that new zombie and avoid having to write extra code right?
I mean this class was really just for tracking the zombie’s target and model. But yes, if you really wanted to (and I mean REALLY, inheritance can be a headache to figure out), then yes you can set up inheritance for different zombie classes.
But realistically composition would be much easier. For example, have a Character base class, then make a Zombie component and Explosive component. The Zombie component has code to make the Character follow its target, and then Explosive component simply listens to when it dies, and then spawns an explosion at its location.
Okay, just to clear it up I could basically do it like this as an example. If I were to make a zombie that charges at the player:
local zombie = {}
zombie.__index = zombie
function zombie.new()
local self = setmetatable({}, zombie)
return self
end
local chargeAbility = {}
chargeAbility.__index = chargeAbility
function chargeAbility.new(zombieModel: model)
local self = setmetatable({}, chargeAbility)
return self
end
function chargeAbility:charge()
self.zombieModel.humanoid:MoveTo(Vector3.new(0, 0, 100)) -- do charging stuff
end
local chargerZombie = {}
chargerZombie.__index = chargerZombie
function chargerZombie.new()
local self = setmetatable({}, zombie)
self.zombieComponent = zombie.new() -- something like this??
self.chargingAbility = chargeAbility.new()
return self
end
Would it look something like this?
I was thinking more of passing the zombie’s object into the component, like this:
local self = setmetatable({}, chargerZombie)
local zombieComponent = zombie.new(self)
local chargingAbility = chargeAbility.new(self)
Storing the components in the object would completely depend on how you use the components, but it’s probably better to store them if you wanted to access them later.
Okay, I’ll try this system, thanks for all your help!
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.