What do you want to achieve? I want to run my function in a module tween whenever module.Notify needs to tween something.
What is the issue? I cannot use this function from within the same module.
What solutions have you tried so far?This page said that I can do this, but my issue with this approach is that it seems that I would have to either change the name of my function that is being used by other non-module scripts, or create a duplicate with the name listed there.
Module Script
--Services
local TweenService = game:GetService("TweenService")
local TeleportService = game:GetService("TeleportService")
local Players = game:GetService("Players")
local module = {}
local function tween(instance,tweenInfo,propertyTable,tweenCompleteFunction)
local tweenTask = function()
local tween = TweenService:Create(instance,tweenInfo,propertyTable)
tween:Play()
tween.Completed:Wait()
if tweenCompleteFunction then
tweenCompleteFunction()
end
end
task.spawn(tweenTask)
end
module.Notify = function(notificationsContainer, title, description)
local newNotification = notificationsContainer.Template:Clone()
newNotification.Title.Text = title
newNotification.Description.Text = description
newNotification.Visible = true
for _, notification in pairs(notificationsContainer:GetChildren()) do
if notification.Name ~= "Template" then
tween(notification,TweenInfo.new(.1,Enum.EasingStyle.Back),{Position = notification.Position + UDim2.new(0,0,.25,0)})
end
end
newNotification.Parent = notificationsContainer
end
I’ve only included what I think is necessary in the script for simplicity, but I’d be glad to share more info if needed for any reason.
I’m not entirely certain what you are trying to do. Are you saying the function tween isn’t working for you or are you saying you want to call module.Notify in your script? Both should work, but I’m not sure what you are having issues with.
The only potential issue I can see is if you are trying to call tween() it’s possible the if statement it is contained in skips it. Try putting a print above the function you are calling to make sure the code gets to that line.
I’ve realized that the current code contradicts what I meant to be saying. I can now call the function from the module script, it was just that the “if” statement wasn’t true.
My issue now is that I can’t call module.tween from other scripts, as it’s a local function here.
Hey, wanna bump this and clarify what’s going on.
I want to call a function in a ModuleScript from inside the ModuleScript, as well as from scripts requiring the ModuleScript.
I currently know how to do this one way or another: either,
Setting the function I want to use as a local function, resulting in not being able to use it through require
Setting the function I want to use as ```module.(function name) = function(), which results in not being able to use it in that same module script.
The forum page (Check original message, avoiding double notification) I linked seems like it would result in functions defined not being able to be accessed by require, if I’m wrong, please tell me.
There’s another way that a function can be added to the module which would also enable it to be called from the ModuleScript itself and other scripts that have required it.
local module = {}
function module.ExampleFunction1() -- This can be called from inside the ModuleScript and from scripts that require it
print("ExampleFunction1 has been activated")
end
However, it is still possible to define it through module.variableName = function() and be called from the same ModuleScript. Here’s another example:
local module = {}
function module.ExampleFunction1()
print("ExampleFunction1 has been activated")
end
module.testFunction = function()
print("testFunction has been activated")
end
-- Once this module has been required, both of the functions will be called and both prints will appear in the Output
-- Even if this was removed from the ModuleScript and instead included in a script that was requiring it, both functions would still be activated
module.ExampleFunction1()
module.testFunction()
return module
I cannot replicate this, and am getting an error (attempt to call a nil value) when trying to run tween inside of the modulescript.
Here's an updated version of my ModuleScript
local module = {}
function module.tween(instance,tweenInfo,propertyTable,tweenCompleteFunction)
local tweenTask = function()
local tween = TweenService:Create(instance,tweenInfo,propertyTable)
tween:Play()
tween.Completed:Wait()
if tweenCompleteFunction then
tweenCompleteFunction()
end
end
task.spawn(tweenTask)
end
module.Notify = function(notificationsContainer, title, description)
for _, notification in pairs(notificationsContainer:GetChildren()) do
if notification.Name ~= "Template" then
tween(notification,TweenInfo.new(.5,Enum.EasingStyle.Linear),{Position = UDim2.new(0,0,notification.Position.Y.Scale - .25,0)})
end
end
local newNotification = notificationsContainer.Template:Clone()
newNotification.Title.Text = title
newNotification.Description.Text = description
newNotification.Name = "Test"
newNotification.Visible = true
wait(.5)
tween(newNotification,TweenInfo.new(.25,Enum.EasingStyle.Linear),{Position = UDim2.new(0,0,1,0)})
newNotification.Parent = notificationsContainer
end
Is the error originating from this line of code, toward the bottom of the ModuleScript?
If so, that’s occurring because the function called “tween” is contained within the table called “module”, which means that it would need to be called via module.tween().
It works in the same way as calling the function from another script because when another script is requiring the ModuleScript, it returns the table which has the function in it.