Collection Service doesnt work with cloned models

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I have a train game and with it i added a train spawner so that when you click the button it spawns a train you can drive

  2. What is the issue? Include screenshots / videos if possible!
    i use collection service for the train chassis to save performance but when i spawn my train using :Clone() the train cant be driveable and the gui doesnt pop up

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i’ve tried using on tag added but it just doesnt work with my script
    the devforum also didnt help
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local CollectionService = game:GetService("CollectionService")
local Trains = CollectionService:GetTagged("Trains")

for i, model in pairs(Trains) do
	local bin = model.V1
	local eng = { }
	local st = bin.VehicleSeat
	local msp = bin.Values.MaximumSpeed.Value
	st.MaxSpeed = msp
	local mrs = bin.Values.MaximumReverseSpeed.Value --> Maximum Reverse Speed
	local spt = bin.Values.SpeedPerThrottle.Value --> Speed Per Throttle. This is how much each notch on the throttle changes
	local mxt = math.ceil(msp / spt) --> This is the maximum throttle
	local crt = 0 --> Current throttle
	local csd = 0 --> Current speed
	local cdr = 1 --> Current direction
	local drive = false --> Is the train driving. False = not driving, true = driving
	local plr = nil --> This will change to the current driver
	local DirTable = {"Reverse", "Neutral", "Foward"}
	local TrtStp = 1 --> This is used so there is a second or 2 gap in between changing the throttle.
	
	function GetEngines() --> This finds all the engines in the train and then puts them into the table 'eng'
		for _, a in pairs(bin:GetChildren()) do
			if (a.Name == "Engine") then
				--if a:findFirstChild("BodyVelocity") then
				local bv = bin.Values.BodyVelocity:Clone()
				bv.Name = "Motor"
				bv.Parent = a
				eng[#eng + 1] = a
				--end
			end
		end
	end 

	function GetDirString() return DirTable[cdr + 2]; end

	function GetSpeedString(a)
		if (a < 0) then return "-"..a.."" end
		return msp
	end

	function GetPlayer()
		local a = st.SeatWeld.Part1
		if a then
			local p = game.Players:findFirstChild(a.Parent.Name)
			if p then
				return p
			end
		end
		return false
	end

	function GetHint(p)
		-- local a = p:findFirstChild("TrainDriveHint")
		local a = p.PlayerGui:findFirstChild("Dualis")
		if a then return a; end
		return
	end

	wait(1)
	GetEngines()

	h = bin.Values.Dualis:Clone()

	h2 = h:Clone();

	st.ChildAdded:connect(function () 
		drive = true 
	--[[local cam = script.Cam:Clone()
	cam.Disabled = false
	if GetPlayer() then
		cam.Parent = GetPlayer().Backpack
	end--]]
	end)

	st.ChildRemoved:connect(function () 
		drive = false

		if GetHint(plr) then 
			GetHint(plr).Parent = nil 
			h = bin.Values.Dualis:Clone()
			h2 = h:Clone()

		end
	end)

	while true do
		wait(0.0000000005)
		if drive then
			local p = GetPlayer()
			if p then
				plr = p 
				local gh = GetHint(p)
				if not gh then h2.Parent = p.PlayerGui end
				local h2 = GetHint(p)
				--Now to calculate the current throttle
				local trt, dir = st.Steer, st.Throttle
				if (TrtStp > 0) then
					TrtStp = TrtStp - 0.5
				else
					if (trt == -1) then
						if (crt > 0) then crt = crt - 1; TrtStp = 1; end
					elseif(trt == 1) then
						if (crt < mxt) then crt = crt + 1; TrtStp = 1; end
					end
				end
				--Throttle calculated, now get the direction
				cdr = cdr + dir
				if (cdr > 1) then cdr = 1 end
				if (cdr < -1) then cdr = -1 end
				--Direction done, now work out the speed			
				if (crt == 0) then
					if (csd < 0) then
						if (csd + 0.5 > 0) then
							csd = 0
						else
							csd = csd + 0.5
						end
					elseif (csd > 0) then
						if (csd - 0.5 < 0) then
							csd = 0
						else
							csd = csd - 0.5
						end
					end					
				end
				if (crt > 0) then
					if (cdr == -1) then
						if (csd - (crt / 10) < (spt * crt) * -1) then 
							csd = (spt * crt) * -1
						else
							csd = csd - (crt / 10)
						end
						csd = csd - (crt / 10)
						if (csd < (spt * crt) * -1) then csd = csd + 0.5 end
						if (csd < (mrs * -1)) then csd = mrs  * -1 end
						if (csd > 0) then csd = csd * -1 end
					elseif (cdr == 1) then
						if (csd + (crt / 10) > spt * crt) then 
							csd = spt * crt
						else
							csd = csd + (crt / 10)
						end
						if (csd > spt * crt) then csd = csd - 0.5 end
						if (csd > msp) then csd = msp end
						if (csd < 0) then csd = csd * -1 end
					end
				end
				for _, a in pairs(eng) do
					a:WaitForChild("Motor").velocity = a.CFrame.lookVector * csd
				end

				if crt >= 0 then
					h2.MainDriving.Manipulateur.Throttle.Text = crt
				elseif crt < 0 then
					h2.MainDriving.Manipulateur.Throttle.Text = -crt
				end
				if cdr == -1 then
					h2.MainDriving.Manipulateur.Direction.Text = "Marche Arrière"
				elseif cdr == 1 then
					h2.MainDriving.Manipulateur.Direction.Text = "Marche Avant"
				elseif cdr == 0 then
					h2.MainDriving.Manipulateur.Direction.Text = "Neutre"
				else
					h2.MainDriving.Manipulateur.Direction.Text = "N/A"
				end

			end
		end
	end


	plr.Character.Humanoid.Died:connect(function()
		drive = false

		if GetHint(plr) then 
			GetHint(plr).Parent = nil 
			h = bin.Values.Dualis:Clone()
			h2 = h:Clone()

		end
	end)
	plr.Character.Humanoid.Jumping:connect(function()
		drive = false
		if GetHint(plr) then 
			GetHint(plr).Parent = nil 
			h = bin.Values.Dualis:Clone()
			h2 = h:Clone()
		end
	end)
end
1 Like

calling CollectionService:GetTagged() will only give you the instances that were currently tagged when you called the function. It will not update as you tag new instances. To fix this, try calling CollectionService:GetTagged() whenever you need to get all instances with a tag instead of caching it in a variable at the top of your script

Can you put in an example? Its my first time using the service so im not very familiar with it

1 Like

Instead of doing

local Collection = CollectionService:GetTagged("Tag")
function DoSomethingWithCollection()
    for i,v in Collection do
        -- do whatever here
    end
end

Get all tagged instances every single time you’re interacting with them

function DoSomethingWithCollection()
    local Collection = CollectionService:GetTagged("Tag")
    for i,v in Collection do
        -- do whatever here
    end
end