Hello! No matter what I do, this run service only adds Enemies to the Kill reg that are already in the Folder in the workspace that were there Prehand. Not when new enemies are added:
RunService.Heartbeat:Connect(function(delta)
Check += delta
if Check > 3 then
print("Checking For Enemies..")
Check = 0
for i,v in pairs(CreaturesFolder:GetChildren()) do
if v:IsA("Model") and v:FindFirstChild("Humanoid") and v:IsDescendantOf(CreaturesFolder) then
if v.Humanoid:FindFirstChild("InKillReg") then return end
warn("Creature Found: " .. tostring(v) .. " Adding to KillReg")
CreatureAdded(v)
else
warn("No Humanoids Found In Enemies Folder")
end
end
end
end)
It never adds new enemies into the kill reg, I don’t need to paste the full script since the only issue is the RunService. (It only prints " wanr(“Crteature Found: " … tostring(v) … " Adding to KillReg”)" when there’s something in that folder already not when something is added to it. Same issue occurs when I try to do .ChildAdded (It doesn’t work)
Last time I used it didn’t do anything at all when I added a new Child, if it helps any the Script is in ServerScriptStorage but I doubt that’ll make any difference.
Are you sure you’re connected to the correct folder and that folder is having its instances added correctly? ChildAdded shouldn’t be failing to fire like that unless one of those two things has occurred.
Allthough sometimes I’ll be real with you when evber I try to do something new it doesn’t work at all at the start then randomly later down the line it just starts working for me, I am really not sure like if it’s a me problem or so. I am probably going to guess I am going to try to do .ChildAdded and it’s going to work.
Assuming your CreaturesFolder reference is correct, is there anything above that connection that may prevent it from establishing? (Ex: Infinite loop that yields luau)
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local CreaturesFolder = workspace:WaitForChild("Creatures")
local Debris = require(ReplicatedStorage.Modules.BetterDebris)
local function CreatureAdded(Creature)
local CreatureHumanoid = Creature:FindFirstChild("Humanoid")
local AlreadyInKillReg = Instance.new("BoolValue")
AlreadyInKillReg.Name = "InKillReg"
AlreadyInKillReg.Value = true
AlreadyInKillReg.Parent = CreatureHumanoid
if not CreatureHumanoid then warn("Selected Creature: " .. tostring(Creature .. " Does not have a Humanoid ")) return end
Creature.Humanoid.Died:Connect(function()
Debris.AddItem(Creature, 4)
for _, v in ipairs(CreatureHumanoid:GetChildren()) do
if v.Name == "EnemyDamage" then
local dmg = v:GetAttribute("Damage")
local player = v:GetAttribute("Player")
if not (dmg and player) then return end
local Killer = game.Players:FindFirstChild(player)
if Killer then
local CrystalAmount = CreatureHumanoid:GetAttribute("Crystals")
print("Creature Killed Rewarding " .. CrystalAmount .. " crystals")
Killer.leaderstats.Crystal.Value += CrystalAmount
end
end
end
end)
end
local Check = 0
CreaturesFolder.ChildAdded:Connect(function(ChildAdded)
print(ChildAdded)
end)
RunService.Heartbeat:Connect(function(delta)
Check += delta
if Check > 3 then
print("Checking For Enemies..")
Check = 0
for i,v in ipairs(CreaturesFolder:GetChildren()) do
if v:IsA("Model") and v:FindFirstChild("Humanoid") and v:IsDescendantOf(CreaturesFolder) then
if v.Humanoid:FindFirstChild("InKillReg") then return end
warn("Creature Found: " .. tostring(v) .. " Adding to KillReg")
CreatureAdded(v)
else
warn("No Humanoids Found In Enemies Folder")
end
end
end
end)
This is the entire script, I am pretty sure nothing here Cuases a yield of any sort. that I know of.
I checked the ChildAdded, it functions just fine on my end. Are you ABSOLUTELY SURE that CreaturesFolder is defined correctly and there isn’t any yields for the :WaitForChild() calls at the top?
local CreaturesFolder = workspace:WaitForChild("Creatures")
local Debris = require(ReplicatedStorage.Modules.BetterDebris)
if not CreaturesFolder then
error("Folder Not Found")
else
warn("Folder Found")
end