Ok so I discovered an extremely hacky fix for this (still make sure to check out the documentation of scopes Cairo linked Not Identifying Instances in Different Functions - #2 by xGOA7x): make a globally scoped variable with any number or string it doesn’t matter then later in your script when you want to make the new instance take that variable and set it to the new instance like this,
testvar = Instance.new("part")
and there you go you have just made a new instance that can be referenced in any function of the script (dont rely on this much cause I figured it out in like 15 minutes and Im not sure what liabilities come with doing this but from my testing it seemed to work just like creating any other new instance)
Like @HugeCoolboy2007 said, defining a variable outside of a function is not a hacky solution. Variables created inside of functions are not accessible because they are created locally inside that function. By defining the variable before hand, you are allowing both the function and any code outside the function to access that variable.
local GlobalVariable = true --Variables defined on the outside are "global"
function UpdateVar(State1, State2)
GlobalVariable = State1 --Defined before; accessible inside and outside
local LocalVariable = State2 --Defined inside; only accessible inside
print (LocalVariable) --Prints true because it was defined in this function
end
UpdateVar(false, true)
print(GlobalVariable) --Prints false because it was assigned outside the function
print(LocalVariable) --Throws an error because it was never defined outside the function
I am fairly new to scripting so I guess I shouldn’t call things hacky if I dont really know what Im talking about I just thought that it was odd how you had to have a premade variable in order to create new instances that are seen by the rest of the script so thanks for the input.