This is extremely frustrating that it is forcing me to print my function as a variable instead of just being able to call them like Func1() and Func2(). Why is it forcing me to store it as a variable? Why is it forcing me to print it to be able to run it… This isn’t making sense please help someone thank you very much
local function Func1()
print("Hi")
end
local function Func2()
print("Hello")
end
local Func1 = Func1()
local Func2 = Func2()
print(Func1)
print(Func2)
Neither the Func1 or Func2 return any values, so the Func1 and Func2 variables will be set to nil
The Func1 and Func2 variables are overwriting the Func1 and Func2 function since they share the same name
Essentially what your code is doing is:
Creates two local functions named Func1 and Func2
Create a local variable also named Func1 with the value returned by the Func1 function, which is nil, thus the Func1 function will no longer exist since the variable and the function share their name
Create a local variable also named Func2 with the value returned by the Func2 function, which is nil, thus the Func2 function will no longer exist since the variable and the function share their name
This is the code that isn’t working. It forces you to print and store functions as variables. I would greatly appreciate the help if you can. I know this is a bit confusing. Thank you so much!
local Words = {
["Hi"] = 50,
["Hello"] = 50
}
local Secret_Words = {
["Secret1"] = 1,
["Secret2"] = 99
}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.Event
local Table1 = {}
local Table2 = {}
local function Func1()
local random = math.random(1, 100)
local Chosen = Table1[random]
return Chosen
end
local function Func2()
return Table2[math.random(1, #Table2)]
end
for object_name, frequency in pairs(Words) do
for i = 1, frequency do
table.insert(Table1, object_name)
end
end
for object_name, frequency in pairs(Secret_Words) do
for i = 1, frequency do
table.insert(Table2, object_name)
end
end
PurchaseEvent.OnServerEvent:Connect(function(player)
if player then
task.wait(0.03)
local Func1 = Func1()
local Func2 = Func2()
print(Func2)
if Func2 == "Secret2" then
print(Func1)
elseif Func2 == "Secret1" then
print(Func1)
else
warn("Purchase Failed")
end
end
end)
The issue happens to be the one mentioned above, it’s generally best to give a variable a name that isn’t shared with something else:
local Words = {
["Hi"] = 50,
["Hello"] = 50
}
local Secret_Words = {
["Secret1"] = 1,
["Secret2"] = 99
}
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PurchaseEvent = ReplicatedStorage.Event
local Table1 = {}
local Table2 = {}
local function Func1()
local random = math.random(1, 100)
local Chosen = Table1[random]
return Chosen
end
local function Func2()
return Table2[math.random(1, #Table2)]
end
for object_name, frequency in pairs(Words) do
for i = 1, frequency do
table.insert(Table1, object_name)
end
end
for object_name, frequency in pairs(Secret_Words) do
for i = 1, frequency do
table.insert(Table2, object_name)
end
end
PurchaseEvent.OnServerEvent:Connect(function(player)
if player then
task.wait(0.03)
local value1 = Func1()
local value2 = Func2()
print(value2)
if value2 == "Secret2" then
print(value1)
elseif value2 == "Secret1" then
print(value1)
else
warn("Purchase Failed")
end
end
end)
Now the return value of Func1 is named value1 and the return value of Func2 is named value2, which won’t cause problems with the function themselves being set to nil or a string value, which would prevent them from working more than once
This was the solution! The person who helped me did not realize that it is always a bad idea to give variables a name that could be shared with something else. Thank you so much. You saved me from hours of frustration. Have a wonderful day