Hello developers!
I am having some trouble with not braining on how to decrease delay for destroying instance from object and table, script was quick made so any suggestions acceptable. Thank you.
local plr = game.Players.LocalPlayer
wait(2)
local char = plr.Character
local camera = workspace.CurrentCamera
local SourceLight = workspace:WaitForChild("HeadLight")
local RunService = game:GetService("RunService")
local mouse = game.Players.LocalPlayer:GetMouse()
local TweenService = game:GetService("TweenService")
local allHighlights = {}
local magnitudeFunction = game.ReplicatedStorage.Events.Magnitude
local previousObject
local function HighlightInteraction(object)
local highlight = Instance.new("Highlight")
local tweenIn = TweenService:Create(highlight, TweenInfo.new(0.5), {OutlineTransparency = 0.5})
local tweenOut = TweenService:Create(highlight, TweenInfo.new(0.5), {OutlineTransparency = 0})
highlight.FillTransparency = 1
highlight.OutlineColor = Color3.new(1, 1, 1)
highlight.Parent = object
local inTable = table.insert(allHighlights, highlight)
local tweenTransparency = coroutine.wrap(function()
repeat --Any better alternative?
tweenIn:Play()
tweenIn.Completed:Wait()
tweenOut:Play()
tweenOut.Completed:Wait()
until not highlight
warn("Completed tweenTrasnparency")
end)
tweenTransparency()
end
RunService.RenderStepped:Connect(function()
if SourceLight.SpotLight.Enabled == true then
local tween = TweenService:Create(SourceLight, TweenInfo.new(0.1), {CFrame = camera.CFrame})
tween:Play()
end
local object = mouse.Target
if object and previousObject ~= object then
if object:FindFirstChild("CanBeOpen") then
if plr:DistanceFromCharacter(object.Position) <= 8 then
mouse.Icon = "rbxassetid://16722134267"
HighlightInteraction(object)
elseif plr:DistanceFromCharacter(object.Position) >= 8 then
mouse.Icon = "rbxassetid://16722231082"
end
elseif object:FindFirstChildOfClass("ClickDetector") then
local cd = object:FindFirstChildOfClass("ClickDetector")
if plr:DistanceFromCharacter(object.Position) <= cd.MaxActivationDistance then
mouse.Icon = "rbxassetid://16722134267"
HighlightInteraction(object)
elseif plr:DistanceFromCharacter(object.Position) >= cd.MaxActivationDistance then
mouse.Icon = "rbxassetid://16722231082"
end
else
mouse.Icon = "rbxassetid://16722231082"
if #allHighlights > 0 then --Problem--
for i,v in ipairs(allHighlights) do
v:Destroy()
table.remove(allHighlights, i)
end
print("Active highlights:", #allHighlights)
end
end
end
previousObject = object
end)