Okay so, for context, I’m making a tower defense game based on the hit RPGMaker game OMORI (I know very creative).
Similarly to the actual RPG, I am recreating the emotion system mechanic as seen in the game. Basically, the player uses an item to inflict one of four emotions onto a tower of their own. These emotions consists of:
NEUTRAL – The default emotion, returns the tower’s default stats.
ANGRY – 25% more tower damage, 25% longer attack cooldown.
HAPPY – 25% more tower range, 25% less attack cooldown, 25% more enemy speed.
SAD – 25% less tower damage, 25% less enemy speed.
I’ve already implemented everything about the tower stat changes. The system works fine there, HOWEVER, I can’t seem to figure out how to successfully implement the WalkSpeed changes for the enemies/mobs when they’re in the range of a HAPPY or SAD tower. I can get close yet so far.
(Note: To create the tower defense system, I’ve followed GnomeCode’s tower defense game tutorial series. Although, the emotion system is entirely my own code.)
(Also note: I am not new to scripting, so I do know a little about what I’m doing although I would like another person’s input on this topic before I start growing gray hairs.)
MobEmotionHandler (ServerScript in ServerScriptService)
local CollectionService = game:GetService("CollectionService")
local function applyEffect(mob, outlineColor, fillColor)
for _, child in ipairs(mob:GetChildren()) do
if child:IsA("Highlight") and child.Name == "EffectHighlight" then
child:Destroy()
end
end
if outlineColor and fillColor then
local effect = Instance.new("Highlight")
effect.Name = "EffectHighlight"
effect.OutlineColor = outlineColor
effect.FillColor = fillColor
effect.FillTransparency = 0.75
effect.DepthMode = Enum.HighlightDepthMode.Occluded
effect.Parent = mob
end
end
game:GetService("RunService").Heartbeat:Connect(function()
for _, v in pairs(workspace:WaitForChild("CurrentTowers"):GetChildren()) do
for _, c in pairs(workspace:WaitForChild("CurrentMobs"):GetChildren()) do
local origSpeed = c:WaitForChild("Humanoid").WalkSpeed
if CollectionService:HasTag(c, "IsHappy") then
origSpeed = origSpeed / 1.25
elseif CollectionService:HasTag(c, "IsSad") then
origSpeed = origSpeed / 0.75
end
local happySpeed = origSpeed * 1.25
local sadSpeed = origSpeed * 0.75
local range = v:WaitForChild("Config").Range.Value
local distance = (c.PrimaryPart.Position - v.PrimaryPart.Position).Magnitude
if distance <= range then
if v:WaitForChild("Config").IsHappy.Value == true and not CollectionService:HasTag(c, "IsHappy") then
print(v.Name.." made "..c.Name.." HAPPY with a new speed of ["..happySpeed.."]")
CollectionService:AddTag(c, "IsHappy")
applyEffect(c, Color3.new(0.8, 0.8, 0.2), Color3.new(1, 1, 0))
c:WaitForChild("Humanoid").WalkSpeed = happySpeed
elseif v:WaitForChild("Config").IsSad.Value == true and not CollectionService:HasTag(c, "IsSad") then
print(v.Name.." made "..c.Name.." SAD with a new speed of ["..sadSpeed.."]")
CollectionService:AddTag(c, "IsSad")
applyEffect(c, Color3.new(0.1, 0.2, 0.8), Color3.new(0, 0, 1))
c:WaitForChild("Humanoid").WalkSpeed = sadSpeed
end
elseif distance > range then
if CollectionService:HasTag(c, "IsHappy") then
CollectionService:RemoveTag(c, "IsHappy")
applyEffect(c)
c:WaitForChild("Humanoid").WalkSpeed = origSpeed
elseif CollectionService:HasTag(c, "IsSad") then
CollectionService:RemoveTag(c, "IsSad")
applyEffect(c)
c:WaitForChild("Humanoid").WalkSpeed = origSpeed
end
end
end
end
task.wait(0.1)
end)
I hope I provided enough info for someone to help although if there is more you wish to see please comment and I’ll see if I could show more.