so i have this LocalScript here. it gets objects in the field of view and is supposed to toggle the visibility. the problem, however, is when i try to use TweenService to toggle the transparency of the objects within the tagged model, specifically in the “set_transparency” helper function. it just… never calls the function? i find that to be quite odd.
the script in question:
-- handles tags & stuff
local run_service = game:GetService("RunService")
local collection_service = game:GetService("CollectionService")
local tween_service = game:GetService("TweenService")
local tool = script.Parent
local main_part = tool:WaitForChild("Handle")
local decal = main_part:WaitForChild("normal")
local original_transparency = {}
local rbx_connection
local character
local can_see_tx = 16569627204
local cannot_see_tx = 10198213112
local enemy_tag = "invisible_enemy"
local visibility_tag = "is_visible"
local visible_enemies = {} -- table to store visible enemies
local vision_distance = 60
local field_of_view = 90
local vision_in_front_only = true
local function target_in_vision(target, main_part)
if target and target:IsA("Model") and target.PrimaryPart then
local origin = main_part.Position
local targetPosition = target.PrimaryPart.Position
local direction = (targetPosition - origin).unit * vision_distance
local ray = Ray.new(origin, direction)
local ignore_list = {main_part}
local hit, _ = workspace:FindPartOnRayWithIgnoreList(ray, ignore_list)
if hit and target.PrimaryPart then
if hit:IsDescendantOf(target) then
if vision_in_front_only then
local unit = (targetPosition - origin).Unit
local is_looking = main_part.CFrame.LookVector
local dot = unit:Dot(is_looking)
local half_fov = math.rad(field_of_view) / 2
local max_dot = math.cos(half_fov)
if dot > max_dot then
return true
end
else
return true
end
end
end
end
return false
end
local function update_visible_enemies()
for _, enemy in ipairs(collection_service:GetTagged(enemy_tag)) do
local is_visible = target_in_vision(enemy, main_part)
local was_visible = visible_enemies[enemy]
if is_visible and not was_visible then
visible_enemies[enemy] = true
collection_service:AddTag(enemy, visibility_tag)
elseif not is_visible and was_visible then
visible_enemies[enemy] = nil
collection_service:RemoveTag(enemy, visibility_tag)
end
end
end
local function on_equip()
print("tool equipped")
rbx_connection = run_service.RenderStepped:Connect(function()
update_visible_enemies()
-- check if there are any visible enemies
if next(visible_enemies) then
decal.Texture = ("rbxassetid://" .. tostring(can_see_tx)) -- can see target
else
decal.Texture = ("rbxassetid://" .. tostring(cannot_see_tx)) -- cannot see target
end
end)
end
local function on_unequip()
print("tool unequipped")
if rbx_connection then
rbx_connection:Disconnect()
end
for _, enemy in ipairs(collection_service:GetTagged(enemy_tag)) do
if collection_service:HasTag(enemy, visibility_tag) then
collection_service:RemoveTag(enemy, visibility_tag)
end
end
end
tool.Equipped:Connect(on_equip)
tool.Unequipped:Connect(on_unequip)
local function toggle_visibility(obj: Instance, is_visible: boolean)
local function set_transparency(instance, transparency)
-- stupid dumb function that doesn't want to co-operate with me
print("FIRE IN THE HOLE") -- never prints at all???
if instance:IsA("BasePart") or instance:IsA("MeshPart") or instance:IsA("UnionOperation") or instance:IsA("Part") then
local tween = tween_service:Create(instance, TweenInfo.new(0.2, Enum.EasingStyle.Circular, Enum.EasingDirection.InOut), {Transparency = transparency})
tween:Play()
end
end
if obj:IsA("Model") then
for _, child in ipairs(obj:GetDescendants()) do
if original_transparency[child] then
set_transparency(child, is_visible and original_transparency[child] or 1)
end
end
elseif original_transparency[obj] then
set_transparency(obj, is_visible and original_transparency[obj] or 1)
end
end
collection_service:GetInstanceAddedSignal(visibility_tag):Connect(function(obj)
local highlight = Instance.new("Highlight")
highlight.Parent = obj
-- toggle_visibility(obj, true)
end)
collection_service:GetInstanceRemovedSignal(visibility_tag):Connect(function(obj)
if obj:FindFirstChildWhichIsA("Highlight") then
obj:FindFirstChildWhichIsA("Highlight"):Destroy()
end
-- toggle_visibility(obj, false)
end)