Model health: Collection Services issue

  1. What I want to achieve is that when a model is cloned from server storage to workspace with its collection service tag I want it to still function. I think because the script already ran and did its thing while some were still cloning or something that any new change does not occur.

  2. What is the issue? The issue is that when the model is cloned from storage it gets tagged which is represented by “Health” but when I go to set it to 0 it does not work. When its in workspace without being cloned it does work but when cloned it does not.
    example below
    help

  3. What solutions have you tried so far? I looked all over
    and had no luck, I did see players with simulator issues get fixed but don’t understand collection services enough to know how to fix it. It also does work if you set a (wait) on it but than only works after that wait is over and wont work again unless script is gone.

Also if anyone knows a better way of doing this Its appreciated.

at the start when you check it part is a Part … part:IsA(“part”) I do not think that’s how it works, instead of “Part” you need to type “BasePart”

also when you check if part has Property called Health instead of WaitForChild you should do part:FindFirstChild()

also you have some not very useful checks like if part:WaitForChild(“Health”) then you just put Health into that part so of course it will be there, there is no need to check it again. Then you procced to make new variable for healt. local health = part.Health this is exactly the same as above variable which was healthValue.

local CollectionService = game:GetService("CollectionService")
local GetTags = CollectionService:GetTagged("Breakables")

local function keepTrackOfHP(part,CustomHP)
	task.spawn(function()
		local healthVal = Instance.new("NumberValue",part)
		healthVal.Name = "Health"
		healthVal.Value = CustomHP


		healthVal:GetPropertyChangedSignal("Value"):Connect(function()
			if healthVal.Value <= 0 then
				-- do whatever you please
			end
		end)
	end)
end

for _,part in pairs(GetTags) do
	if part:IsA("BasePart") or part:IsA("Model") then
		keepTrackOfHP(part,200)
	end
end

This code may or may not work, depends on what is causing the error as it is not very clear from your code I do not think anything from your current code is causing it, maybe it is the script which takes away the health but test this out and let me know how it goes.

1 Like

Thank you for responding so quickly. I added part because I had a part earlier before trying it with a model and forgot to remove it. So I have tried the script you have written and it performs the same way as my original does. It works when the Model is already stationed in workspace but if you try to clone it from anywhere else to workspace it does not work. Also I do try to look at the output and it does not display anything. My guess is that it runs the script once and anything new that happens after the script is run does not work. So if i was to put a wait in this script you provided than have the item spawn before the wait time it does work but that would be bad to use especially since the models do respawn.

so what happens currently? what should happen and what happens. can you give me like in game example so I can have better understanding as I am very confused. or if you could give me screenshots or something so I am not trying to help you blindly and we are both on the same page

Yeah sorry about that. Lemme try re-explaining.

So lets say the Model starts out in workspace, than the script which is in “SSS” runs and it tags the “Health” inside it and what I want it to do is when the health reaches 0 it performs whatever I put below rather it is to destroy itself or change the color of a model. So it does work.


So as you can see in part 1 when the Model already starts out in the workspace it works perfectly fine. it turns red after hitting 0.

On the other hand I want the model to be cloned from server storage instead of just being in workspace when players first join. So when it is spawned into workspace it does still add the tag “Health” to the model but now when i set it to 0 it does not run what is below.


so as you can see when I had the server clone the part from the storage into workspace and tried setting it to 0 it does not turn red or anything. It doesn’t go through like its not noticing a change in the value or something.

I was told to try using getinstanceaddedsignal which I tried but still don’t work.
CollectionService:GetInstanceAddedSignal but I pretty new to collection service.


Here is another example as well.

I think that this table does not include parts in server storage then, that has to be the only real explanation.

for _,part in pairs(GetTags) do
	if part:IsA("BasePart") or part:IsA("Model") then
                print("this actually runs")
		keepTrackOfHP(part,200)
	end
end

Try adding this print and if it does not print it in the output that means that, the whole part or model itself is not in the table that you were looping thru.

Also I have no idea what this GetTagged and collection service is, only thing I understand is that it groups specific breakable models in your current game. Why do you use this? Why dont you just create an int value and place it in model and call it Health?

Is there any upperhand using collectionservice that I have not heard of

(Back to our thing, if the print doesn’t run that means that the other function has nothing to do with this and only problem we would have is CollectionService not picking up items in serverstorage) (sry for typos i am on phone and autocorrect is off on devforum for some reason)

It does print in output when you load into the game. But yeah I’m also new to it as well so I barely understand.
To quickly answer your question, I recently started using collection services because it makes it much easier to have a system for my models that need health and groups it all for me instead of having to manually do it for every single model. So instead of having to coded out and put into every model this just saves time and makes it more efficient to use if you have a lot of different models or parts you trying to use.

1 Like

So I did manage to figure it out after decluttering and reading more on the “GetInstanceAddedSignal” and thanks to your cleaner version did get it to work. By setting it to “GetInstanceAddedSignal”. Just needed to let it fire the events incase of change.

1 Like

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