I genuinely can’t tell if my brain needs a break or what, but this code to handle kill parts in my game literally don’t work at all, and sometimes only 1 or 2 work.
Context: Working on a randomly generated obby. Was working fine earlier.
local ServerScriptService = game:GetService("ServerScriptService")
local CollectionService = game:GetService("CollectionService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
for _, killPart in ipairs(CollectionService:GetTagged("KillPart")) do
killPart.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local character = hit.Parent
local humanoid = character.Humanoid
humanoid:TakeDamage(100)
end
end)
end
What am I doing wrong here?
Script is enabled
Parts are tagged
Script DOES find all of the parts tagged, figured that out using print statements.
Tagging the parts via script (i’m at a loss atp)
Okay. I was really hoping the script was parented somewhere they can’t run and it would be an easy solution… lol.
I think the fact that the for loop runs while they also get tagged in another script is why it isn’t working. The for loop runs before barely any get tags. I would suggest waiting a certain amount of time in the script for everything to be tagged, say five or eight seconds, or tag everything before running the game so a script isn’t necessary and it works right off the bat.
I’m tagging the parts manually via the tag editor plugin, could it be possible that the parts aren’t being tagged in-game fast enough? The timer suggsted by @AmoraFolf worked but that isn’t ideal, obviously. So that collectionservice function would work for this typa situation?
Maybe something to do with the tags not all being there before the script.
So I may just use CollectionService:GetInstanceAddedSignal()
so I have came up with this script.
function KillPartHit(hit)
if hit.Parent:FindFirstChild("Humanoid") then
local character = hit.Parent
local humanoid = character.Humanoid
humanoid:TakeDamage(100)
end
end
for _, killPart in ipairs(CollectionService:GetTagged("KillPart")) do
killPart.Touched:Connect(KillPartHit)
end
CollectionService:GetInstanceAddedSignal("KillPart"):Connect(KillPartHit)
another fix could be to apply the function after the map is loaded.