How to fix invalid attempt to call nil fucntion?

so i tried making a modulescript where whne the user clicks a gui button it shrinks right. i put it in replicatedstorage and called it UI_Animations. Then i tried requiring it form a localscript where i looped through the all the descendants of a screengui and whenever the player would press a gui button it would shrink. That was the plan. but then it gave me the error “Players.TobiElReyofficial758.PlayerGui.Frames.Shop.Frame.Buttongroup1.AnimateUI:9: attempt to call a nil value”

These are the scripts:

Modulescript:

local animations = {}

Click_Settings = {
	length = 0.2,
	EasingStyle = Enum.EasingStyle.Sine,
	EasingDirection = Enum.EasingDirection.Out,
	Size = 1.2,
}

function animations.onClick(button, group)
	if button:FindFirstChild("Cooldown_Click").Value == false then
		button:FindFirstChild("Cooldown_Click").Value = true
		local info = {}
		info.TweenInfo = TweenInfo.new(
			Click_Settings.length,
			Click_Settings.EasingStyle,
			Click_Settings.EasingDirection,
			0,
			false,
			0
		)
		info.ShrinkSize = UDim2.new(button.Size.X.Scale/Click_Settings.Size,0,button.Size.Y.Scale/Click_Settings.Size,0)
		info.ShrinkTween = tweenService:Create(button, info.TweenInfo, {Size = info.ShrinkSize})
		info.ShrinkTween:Play()
		task.wait(0.5)
		button:FindFirstChild("Cooldown_Click").Value = false
	end
end```

localscript:

```local replicatedStorage = game:GetService("ReplicatedStorage")
local UI_Animations = require(replicatedStorage:WaitForChild('UI_Animations'))
task.wait(5)


for _, button in pairs(script.Parent:GetDescendants()) do
	if button:IsA("GuiButton") then
		button.MouseButton1Click:Connect(function()
			UI_Animations.onClick(button, button.Parent, button.Parent.Parent)
		end)
	end
end```

any fixes?

In the module script To give the function
You have to put a return

return animations

2 Likes

where should i put it? i mean at what line after what?

In the end of your module script.

it still dosen’t work it keeps saying "Key ‘onClick’ not found in table ‘Module3D’

it still dosen’t work it keeps saying "Key ‘onClick’ not found in table ‘Module3D’. i have no idea why.but it keeps saying that inside the localscript.

In your module:

function animations.onClick(button, group)

In your local script:

UI_Animations.onClick(button, button.Parent, button.Parent.Parent)

Why the localscript called with 3 args until the module needs just 2?

i already tried deleting the third arg but it still dosen’t seem to work

Can you try this possible solution script?

Module:

local animations = {}
local tweenService = game:GetService("TweenService")

Click_Settings = {
	length = 0.2,
	EasingStyle = Enum.EasingStyle.Sine,
	EasingDirection = Enum.EasingDirection.Out,
	Size = 1.2,
}

function animations.onClick(button)
	if not button:FindFirstChild("Cooldown_Click") then
		print("Cooldown_Click is not here")
	end
	if button:FindFirstChild("Cooldown_Click").Value == false then
		button:FindFirstChild("Cooldown_Click").Value = true
		local info = {}
		info.TweenInfo = TweenInfo.new(
			Click_Settings.length,
			Click_Settings.EasingStyle,
			Click_Settings.EasingDirection,
			0,
			false,
			0
		)
		info.ShrinkSize = UDim2.new(button.Size.X.Scale/Click_Settings.Size,0,button.Size.Y.Scale/Click_Settings.Size,0)
		info.ShrinkTween = tweenService:Create(button, info.TweenInfo, {Size = info.ShrinkSize})
		info.ShrinkTween:Play()
		task.wait(0.5)
		button:FindFirstChild("Cooldown_Click").Value = false
	end
end

return animations

Local Script:

local replicatedStorage = game:GetService("ReplicatedStorage")
local UI_Animations = require(replicatedStorage:WaitForChild('UI_Animations'))
task.wait(5)


for _, button in pairs(script.Parent:GetDescendants()) do
	if button:IsA("GuiButton") then
		button.MouseButton1Click:Connect(function()
			UI_Animations.onClick(button)
		end)
	end
end

IT still dosen’t work. What in the world. it gives me the same error

what error sended in your output?

Players.TobiElReyofficial758.PlayerGui.Frames.Shop.Frame.Buttongroup1.AnimateUI:9: attempt to call a nil value

have you put this line?

return animations

I have the solution of your problem, i think:

local UI_Animations = require(game.ReplicatedStorage.UI_Animations)
task.wait(5)


for _, button in pairs(script.Parent:GetDescendants()) do
	if button:IsA("GuiButton") then
		button.MouseButton1Click:Connect(function()
			UI_Animations.onClick(button)
		end)
	end
end

whats wrong with that darn module. it still dosen’t work?

Where you put your module? in The ReplicatedStorage
The module it’s named “UI_Animations” right?

I have change some lines in your module (is a test) can you test it:

local animations = {}
local tweenService = game:GetService("TweenService")

Click_Settings = {
	length = 0.2,
	EasingStyle = Enum.EasingStyle.Sine,
	EasingDirection = Enum.EasingDirection.Out,
	Size = 1.2,
}

function animations.onClick(button)
	if not button:FindFirstChild("Cooldown_Click") then
		print("Cooldown_Click is not here")
	end
	if button:FindFirstChild("Cooldown_Click").Value == false then
		button:FindFirstChild("Cooldown_Click").Value = true
		local info = {}
		info.TweenInfo = TweenInfo.new(
			Click_Settings.length,
			Click_Settings.EasingStyle,
			Click_Settings.EasingDirection,
			0,
			false,
			0
		)
		tweenService:Create(button, info.TweenInfo, {Size = UDim2.new(button.Size.X.Scale/Click_Settings.Size,0,button.Size.Y.Scale/Click_Settings.Size,0)}):Play()
		task.wait(Click_Settings.length)
		button:FindFirstChild("Cooldown_Click").Value = false
	end
end

return animations

this is really troubling. i tested it and it dosent work.

Can you try this module changed?:

local animations = {}
local tweenService = game:GetService("TweenService")

Click_Settings = {
	length = 0.2,
	EasingStyle = Enum.EasingStyle.Sine,
	EasingDirection = Enum.EasingDirection.Out,
	Size = 1.2,
}

function animations.onClick(button)
	local cooldown_click = button:FindFirstChild("Cooldown_Click") or Instance.new("BoolValue",button)
	cooldown_click.Name = "Cooldown_Click"
	if cooldown_click.Value == false then
		cooldown_click.Value = true
		tweenService:Create(button, TweenInfo.new(
			Click_Settings.length,
			Click_Settings.EasingStyle,
			Click_Settings.EasingDirection,
			0,
			false,
			0
			), {Size = UDim2.new(button.Size.X.Scale/Click_Settings.Size,0,button.Size.Y.Scale/Click_Settings.Size,0)}):Play()
		task.wait(Click_Settings.length)
		cooldown_click.Value = false
	end
end

return animations

Is UI_Animations.onClick nil based on the error?

Can you confirm that it is a function if you print UI_Animations.onClick without calling it a line above that?