Button to Open Door Script Not Working

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.


The image above is the script within the door model.
Capture603
This is the script in the button.
Capture604

Is that a localscript or script?

I would recommend utilising my door model, that includes a script for tweening the door:
TouchDoorTweening.rbxl (54.8 KB)

Use it because it will probably help…

If you want to associate it with a button just do this:
TouchDoorTweeningButton.rbxl (55.1 KB)

It is a regular script, not module or local

My door is very large and will rotate as it is circular. The button is not in the door model so I don’t know if that would help, sorry

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) 

Hello Kcavileer ! Did you forgot to put TweenInfo.new() Paramaters at lines 6?

So is my first script correct? Is there anything I should change in it?

I noticed that now there is no “attempt to call nil value” error as well, but the door does not work.

Also, should I put the modulescript in serverstorage?