so i’ve got a basic script that toggles the visibliity of the children of models, using CollectionService. but when i try to add tweening to make the game feel better to play, nothing happens?
this is what the prototype of my game looks like so far.
my script:
-- define the only constant
local VISIBLE_ENEMIES_TAG = "visible_enemies"
-- other variables + services
local collection_service = game:GetService("CollectionService")
local enemy_folder = workspace.map:FindFirstChild("enemies")
local run_service = game:GetService("RunService")
local tween_service = game:GetService("TweenService")
local debris = game:GetService("Debris")
local highlight = script.monstrous_highlight
local phone_info = TweenInfo.new(0.1, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
-- make enemies visible/invisible. true = visible, false = invisible
function set_enemy_visibility(enemy, visible)
for _, child in ipairs(enemy:GetChildren()) do
if child:IsA("BasePart") or child:IsA("MeshPart") or child:IsA("UnionOperation") then
if child.Name ~= "HumanoidRootPart" then
local target_transparency = visible and 0 or 1
local current_transparency = child.Transparency
local tween_goal = { Transparency = target_transparency }
local tween = tween_service:Create(child, phone_info, tween_goal)
tween:Play()
print(child.ClassName)
end
end
end
end
-- initialize the invisibility
for _, enemy in ipairs(enemy_folder:GetChildren()) do
if collection_service:HasTag(enemy, "enemy") then
set_enemy_visibility(enemy, false)
else
collection_service:AddTag(enemy, "enemy")
set_enemy_visibility(enemy, false)
end
end
-- may not be particularly efficient but will do for now
function on_instance_added(object)
set_enemy_visibility(object, true)
end
function on_instance_removed(object)
set_enemy_visibility(object, false)
end
-- handle when enemies can or can not be seen by the player's phone
collection_service:GetInstanceAddedSignal(VISIBLE_ENEMIES_TAG):Connect(on_instance_added)
collection_service:GetInstanceRemovedSignal(VISIBLE_ENEMIES_TAG):Connect(on_instance_removed)
-- also detect any npcs that may already have the tag
for _, object in pairs(collection_service:GetTagged(VISIBLE_ENEMIES_TAG)) do
on_instance_added(object)
end