Hi, I have some scripts listed below that I am attempting to use to script a button to open a door. The door and button are separate models and are not connected. I created a global function in the door script containing CFrame, and a clickdetector/mouseclick event in the button script that runs the global function when the button is pressed. I haven’t seen whether the door opened or not, but the output says “attempt to call a nil value” whenever I press the button. My mouse does not change cursor when I hover over the button too, but I think that is normal and I must input a custom cursor.
Can you try putting this line at the top of both scripts? print(script:GetFullName()) that way you can see which runs first. If “the script within the door model” runs first, then the other script hasn’t had a chance to define _G.GateBOpen yet and then it makes sense that it’s nil.
If you want to wait for it to be defined, do while not _G.GateBOpen do wait() end. It would better to use an approach where you don’t have this kind of unknown execution order. The easiest way IMO is to use a ModuleScript. So the script that defines GateBOpen should be a ModuleScript returning that function or a table of functions. So you might change the first script to
local DoorFunctions = {}
function DoorFunctions.GateBOpen()
local tween = TS:Create(--blahblahblah)
tween:Play() --Creating a tween does nothing, you need to play it too.
end
return Door
And the second script would look like
local DoorFunctions = require(game.ServerStorage.DoorFunctions) --Or whereever you want to store the ModuleScript
doorBClickDetector.MouseClick:Connect(DoorFunctions.GateBOpen)
You could use ModuleScripts to just define the order of execution and still use _G to access things like GateBOpen, it’s not how I usually do things but if you want to that’s fine. Just have your original code in the first script, but put it in a ModuleScript, then change your second script to look like
require(game.ServerStorage.DoorFunctions) --Can't set something to equal what `require` returned, because this time the ModuleScript doesn't return anything it just sets some things in _G
doorBClickDetector.MouseClick:Connect(_G.GateBOpen)