Hello,
I want to clone objects in a folder to the workspace, and I want the objects to keep their tag. Another post said you can clone objects with keeping their tag but for me it doesn’t work.
I tried using this:
for i, block in ipairs(RS.Maps.Blocks:GetChildren()) do
local Clone = block:Clone()
Clone.Parent = workspace.Blocks
end
This doesn’t seem to work.
I tried replicating what you’re doing and the tag is cloned every time. If you do print(#CollectionService:GetTagged(“Tag”)) you should see the number going up along with the cloning of the maps. Try printing CollectionService:HasTag(block, “Tag”) as well for each iteration.
Thank you for replying,
I have tried this and it’s printing ‘true’, however the script that does something witht he tags doesn’t detect the object having a tag.
Script:
local CollectionService = game:GetService("CollectionService")
local TaggedParts = CollectionService:GetTagged("Lucky Block")
for _, TaggedPart in ipairs(TaggedParts) do
TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
print("click")
(this is not the whole script)
It doesn’t print ‘click’.
Could you post the entire script that handles tagging and collecting tags?
This works fine on an object that loads in Workspace when playing, otherwise it doesn’t.
local CollectionService = game:GetService("CollectionService")
local TaggedParts = CollectionService:GetTagged("Lucky Block")
local debounce = false
for _, TaggedPart in ipairs(TaggedParts) do
TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
print("click")
if debounce == false then
debounce = true
local Health = TaggedPart.Health
local Open = TaggedPart.Open
if Open.Value == false then
if Health.Value > 0 then
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local Humanoid = Character:FindFirstChild("Humanoid")
local shouldLookAt = Vector3.new(TaggedPart.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, TaggedPart.HumanoidRootPart.Position.Z)
HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
local Animation = script.SmashAnimation
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
wait(0.75)
debounce = false
end
end
local EffectsFolder = TaggedPart.Effect
local Effect1 = EffectsFolder.Area.Sparkles
local Effect2 = EffectsFolder.Core.Attachment.Glow
local Effect3 = EffectsFolder.Core2.Attachment.Rays1
local Effect4 = EffectsFolder.Core2.Attachment.Rays2
local Effect5 = EffectsFolder.Core2.PointLight
local RS = game:GetService("ReplicatedStorage")
local Event = RS:FindFirstChild("Health")
local Health = TaggedPart.Health
local OpenValue = TaggedPart.Open
if OpenValue.Value == false then
if Health.Value > 0 then
Health.Value -= 50
Event:FireClient(Player, Health.Value)
wait(0.5)
if Health.Value < 50 then
local Animation = script:WaitForChild("OpenAnimation")
local AnimationController = TaggedPart:WaitForChild("AnimationController")
local Open = AnimationController:LoadAnimation(Animation)
wait(0.25)
Open:Play()
Effect1.Enabled = true
Effect2.Enabled = true
Effect3.Enabled = true
Effect4.Enabled = true
Effect5.Enabled = true
OpenValue.Value = true
wait(1)
TaggedPart.Top:Destroy()
wait(0.75)
debounce = false
end
end
end
end
end)
TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
local InsertService = game:GetService("InsertService")
local Gears = {
47620, -- Slingshot
47586, -- Time Bomb
246270069, -- Katana
47637, -- Rocket Launcher
}
local Health = TaggedPart.Health
local Open = TaggedPart.Open
if Open.Value == true then
if Health.Value < 50 then
TaggedPart.ClickDetector:Destroy()
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local shouldLookAt = Vector3.new(TaggedPart.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, TaggedPart.HumanoidRootPart.Position.Z)
local Humanoid = Character:FindFirstChild("Humanoid")
local Animation = script.GrabAnimation
local AnimationTrack = Humanoid:LoadAnimation(Animation)
HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
AnimationTrack:Play()
wait(0.4)
local RandomGear = Gears[math.random(1,#Gears)]
local Backpack = Player:WaitForChild("Backpack")
local NewGear = InsertService:LoadAsset(RandomGear)
local Tool = NewGear:FindFirstChildOfClass("Tool")
if Tool then
wait(0.5)
Tool.Parent = Backpack
wait(0.75)
end
end
end
end)
end
You are missing the:
For when a new instance is added or cloned.
I don’t really know how to implement it.
Have you looked at the example code posted? You should try doing something similar.
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
Let me know if you need help.
@dthecoolest should this work?
local connections = {}
local function onInstanceAdded(object)
if object:IsA("Model") then
connections[object] = object.ClickDetector.MouseClick:Connect(onClicked)
end
end
local function onInstanceRemoved(object)
if connections[object] then
connections[object]:disconnect()
connections[object] = nil
end
end
CollectionService:GetInstanceAddedSignal("Lucky Block"):Connect(onInstanceAdded)
for _, object in pairs(CollectionService:GetTagged("Lucky Block")) do
onInstanceAdded(object)
end
My script doesn’t seem to function anymore:
local CollectionService = game:GetService("CollectionService")
local Tag = "Lucky Block"
local TaggedParts = CollectionService:GetTagged(Tag)
local debounce = false
for _, TaggedPart in ipairs(TaggedParts) do
--TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
local function onClicked(Player)
print("click")
if debounce == false then
debounce = true
local Health = TaggedPart.Health
local Open = TaggedPart.Open
if Open.Value == false then
if Health.Value > 0 then
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local Humanoid = Character:FindFirstChild("Humanoid")
local shouldLookAt = Vector3.new(TaggedPart.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, TaggedPart.HumanoidRootPart.Position.Z)
HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
local Animation = script.SmashAnimation
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
wait(0.75)
debounce = false
end
end
local EffectsFolder = TaggedPart.Effect
local Effect1 = EffectsFolder.Area.Sparkles
local Effect2 = EffectsFolder.Core.Attachment.Glow
local Effect3 = EffectsFolder.Core2.Attachment.Rays1
local Effect4 = EffectsFolder.Core2.Attachment.Rays2
local Effect5 = EffectsFolder.Core2.PointLight
local RS = game:GetService("ReplicatedStorage")
local Event = RS:FindFirstChild("Health")
local Health = TaggedPart.Health
local OpenValue = TaggedPart.Open
if OpenValue.Value == false then
if Health.Value > 0 then
Health.Value -= 50
Event:FireClient(Player, Health.Value)
wait(0.5)
if Health.Value < 50 then
local Animation = script:WaitForChild("OpenAnimation")
local AnimationController = TaggedPart:WaitForChild("AnimationController")
local Open = AnimationController:LoadAnimation(Animation)
wait(0.25)
Open:Play()
Effect1.Enabled = true
Effect2.Enabled = true
Effect3.Enabled = true
Effect4.Enabled = true
Effect5.Enabled = true
OpenValue.Value = true
wait(1)
TaggedPart.Top:Destroy()
wait(0.75)
debounce = false
end
end
end
end
end
--TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
local function onClicked2(Player)
local InsertService = game:GetService("InsertService")
local Gears = {
47620, -- Slingshot
47586, -- Time Bomb
246270069, -- Katana
47637, -- Rocket Launcher
}
local Health = TaggedPart.Health
local Open = TaggedPart.Open
if Open.Value == true then
if Health.Value < 50 then
TaggedPart.ClickDetector:Destroy()
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local shouldLookAt = Vector3.new(TaggedPart.HumanoidRootPart.Position.X, HumanoidRootPart.Position.Y, TaggedPart.HumanoidRootPart.Position.Z)
local Humanoid = Character:FindFirstChild("Humanoid")
local Animation = script.GrabAnimation
local AnimationTrack = Humanoid:LoadAnimation(Animation)
HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
AnimationTrack:Play()
wait(0.4)
local RandomGear = Gears[math.random(1,#Gears)]
local Backpack = Player:WaitForChild("Backpack")
local NewGear = InsertService:LoadAsset(RandomGear)
local Tool = NewGear:FindFirstChildOfClass("Tool")
if Tool then
wait(0.5)
Tool.Parent = Backpack
wait(0.75)
end
end
end
local connections = {}
local function onInstanceAdded(object)
if object:IsA("Model") then
connections[object] = object.ClickDetector.MouseClick:Connect(onClicked)
connections[object] = object.ClickDetector.MouseClick:Connect(onClicked2)
end
end
local function onInstanceRemoved(object)
if connections[object] then
connections[object]:disconnect()
connections[object] = nil
end
end
CollectionService:GetInstanceAddedSignal(Tag):Connect(onInstanceAdded)
for _, object in pairs(CollectionService:GetTagged(Tag)) do
onInstanceAdded(object)
end
end
end
Try doing something like this:
while wait(1) do
print(#CollectionService:GetTagged(“Tag”))
end
Post the output afterwards.
No, the problem is that I used this and my script doesn’t work anymore.
That’s not what I’m saying. I’m trying to find the root of the problem. Although using the :GetInstanceAddedSignal is probably good practice, it probably isn’t the reason why your code isn’t running, since if you printed what I asked you to print and it still isn’t showing additional instances tagged then something else is going on.
I printed and it printed ‘true’ so that works.
How could this print true?
while wait(1) do
print(#CollectionService:GetTagged(“Tag”))
end
local CollectionService = game:GetService("CollectionService")
while true do
for i, LuckyBlock in ipairs(script.Parent:GetChildren()) do
print(CollectionService:HasTag(LuckyBlock, "Lucky Block"))
wait(1)
end
end
Oh sorry, but I meant this.
The script does not run because the functions never ran. You can simplify it by right clicking and collapsing all.
Instead of using on InstanceRemoved we can use Ancestry changed in order to detect when the block is destroyed and remove the mouse click connections from there (if they exist)
I don’t believe this is necessary as when the click detector is destroyed the event should be destroyed as well but I’ll keep it in just to be safe.
Script
local CollectionService = game:GetService("CollectionService")
local function setupInstanceAdded(TaggedPart)
local debounce = false
local event1 = TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
print("click")
if debounce == false then
debounce = true
local Health = TaggedPart.Health
local Open = TaggedPart.Open
if Open.Value == false then
if Health.Value > 0 then
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local Humanoid = Character:FindFirstChild("Humanoid")
local shouldLookAt = Vector3.new(
TaggedPart.HumanoidRootPart.Position.X,
HumanoidRootPart.Position.Y,
TaggedPart.HumanoidRootPart.Position.Z
)
HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
local Animation = script.SmashAnimation
local AnimationTrack = Humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
wait(0.75)
debounce = false
end
end
local EffectsFolder = TaggedPart.Effect
local Effect1 = EffectsFolder.Area.Sparkles
local Effect2 = EffectsFolder.Core.Attachment.Glow
local Effect3 = EffectsFolder.Core2.Attachment.Rays1
local Effect4 = EffectsFolder.Core2.Attachment.Rays2
local Effect5 = EffectsFolder.Core2.PointLight
local RS = game:GetService("ReplicatedStorage")
local Event = RS:FindFirstChild("Health")
local Health = TaggedPart.Health
local OpenValue = TaggedPart.Open
if OpenValue.Value == false then
if Health.Value > 0 then
Health.Value -= 50
Event:FireClient(Player, Health.Value)
wait(0.5)
if Health.Value < 50 then
local Animation = script:WaitForChild("OpenAnimation")
local AnimationController = TaggedPart:WaitForChild("AnimationController")
local Open = AnimationController:LoadAnimation(Animation)
wait(0.25)
Open:Play()
Effect1.Enabled = true
Effect2.Enabled = true
Effect3.Enabled = true
Effect4.Enabled = true
Effect5.Enabled = true
OpenValue.Value = true
wait(1)
TaggedPart.Top:Destroy()
wait(0.75)
debounce = false
end
end
end
end
end)
local event2 = TaggedPart.ClickDetector.MouseClick:Connect(function(Player)
local InsertService = game:GetService("InsertService")
local Gears = {
47620, -- Slingshot
47586, -- Time Bomb
246270069, -- Katana
47637, -- Rocket Launcher
}
local Health = TaggedPart.Health
local Open = TaggedPart.Open
if Open.Value == true then
if Health.Value < 50 then
TaggedPart.ClickDetector:Destroy()
local Character = Player.Character or Player.CharacterAdded:Wait()
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart")
local shouldLookAt = Vector3.new(
TaggedPart.HumanoidRootPart.Position.X,
HumanoidRootPart.Position.Y,
TaggedPart.HumanoidRootPart.Position.Z
)
local Humanoid = Character:FindFirstChild("Humanoid")
local Animation = script.GrabAnimation
local AnimationTrack = Humanoid:LoadAnimation(Animation)
HumanoidRootPart.CFrame = CFrame.lookAt(HumanoidRootPart.Position, shouldLookAt)
AnimationTrack:Play()
wait(0.4)
local RandomGear = Gears[math.random(1, #Gears)]
local Backpack = Player:WaitForChild("Backpack")
local NewGear = InsertService:LoadAsset(RandomGear)
local Tool = NewGear:FindFirstChildOfClass("Tool")
if Tool then
wait(0.5)
Tool.Parent = Backpack
wait(0.75)
end
end
end
end)
--
TaggedPart.AncestryChanged:Connect(function(_, parent)
if not parent then
print("object destroyed!")
--disconnect the remaining two
local test = event1 and event1:Disconnect()
local test = event2 and event2:Disconnect()
end
end)
return event1, event2
end
local tag = "Lucky Block"
local TaggedParts = CollectionService:GetTagged(tag)
for _, TaggedPart in ipairs(TaggedParts) do
setupInstanceAdded(TaggedPart)
end
CollectionService:GetInstanceAddedSignal(tag):Connect(setupInstanceAdded)
Edit: On a side note I recommend using functions because they can collapse like so and make your code neater.
Hmm… I just got back to the script today. When I click the Lucky Block once the health value goes down to 0 instantly, it is supposed to go to 50 first (when clicked once).
Send the line of code that sets the health value please.
The issue is that it doesn’t know what part it is clicking, my new thread.