Code literally doesn't run at all

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)

10 Likes

I sound stupid asking this, but what is the parent of the script?

4 Likes

ServerScriptService. I also just tried using code that worked in another place here and it didn’t work…

3 Likes

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.

3 Likes

Tagging the parts manually was something I’ve already done before and was my preferred method.

2 Likes

Was the script still not working then?

3 Likes

Could it be for the same reason as this?

4 Likes

I think?

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?

2 Likes

Indeed that function should work for every case where instances get tagged, unless somehow they are getting tagged before that line runs.

To cover all cases you should use the for loop before this function

3 Likes

dawg tysm i need a break from developing LOL

3 Likes

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.

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.