Why wont it let me set a variable to an instance from a function am I stupid?

Here is my code;

scrpt = nil

local function SelectScript(scrpt)
	if (scrpt == "Server") then
		scrpt = Instance.new("Script")
		config_scriptNameTxtb.Text = "Script"
	elseif (scrpt == "Local") then
		scrpt = Instance.new("LocalScript")
		config_scriptNameTxtb.Text = "LocalScript"
	elseif (scrpt == "Module") then
		scrpt = Instance.new("ModuleScript")
		config_scriptNameTxtb.Text = "ModuleScript"
	end
	
	ScriptChooser.Visible = false
	ScriptConfig.Visible = true
	
	return scrpt
end

servscrptbtn.MouseButton1Click:Connect(function()
	SelectScript("Server")
	return scrpt
end)

why won’t this work? I’m trying to set scrpt to an instance of a script but it says it’s nil if I try to access scrpt.Parent or other properties.

you litterally set scrpt to nil at the top. Which means all the if statements wont work. Remove the scrpt thing at the top and u should be good.

Yes because I initialize a variable that will be set to blank or nil I could do local scrpt instead of local scrpt = nil but it usually fires an error about a blank variable. Also, this wouldn’t cause an error because my if statements do fire it just won’t set the variable it’s something with the return statements I believe.

Just try it. If it doesn’t work then tell me.

just did fired the same error then I provided an explanation why it wouldn’t work.

maybe try to move the return at the end of the if statement like this

local function SelectScript(scrpt)
	if (scrpt == "Server") then
		scrpt = Instance.new("Script")
		config_scriptNameTxtb.Text = "Script"
return scrpt
	elseif (scrpt == "Local") then
		scrpt = Instance.new("LocalScript")
		config_scriptNameTxtb.Text = "LocalScript"
	elseif (scrpt == "Module") then
		scrpt = Instance.new("ModuleScript")
		config_scriptNameTxtb.Text = "ModuleScript"
	end
	
	ScriptChooser.Visible = false
	ScriptConfig.Visible = true
	

end

Found and solved the problem since I was returning twice to two different functions I had no scrpt variable so when I returned out of SelectScript it returned to the call of the function which was another function.