Situation:
In my game I have some doors that should open with a proximity prompt, so I made a script to go through all models with a door tag and give them a proximity prompt that has tweening for opening and closing. The related functions for these are in a separate module
Problem:
I need to have some variables that are defined out of the main function for cmdr, however when the function runs it errors out because it can’t see the variable, despite earlier testing
Note(s):
– Suggestions are appreciated, but please make sure to provide a solution to the original problem with them (if the problem hasn’t already been solved of course)
Scripts:
Main Script:
--[[ Services ]]--
local Services = {
Workspace = game:GetService("Workspace");
Players = game:GetService("Players");
ReplicatedStorage = game:GetService("ReplicatedStorage");
}
--[[ Modules ]]--
local Module: {} = require(script.Parent.CoreConfig.Doors)
--[[ Functions ]]--
local function FindAll( Start: Instance, Class: string, Tag: string, Name: string ): {Instance}
local List = {}
for _, child in pairs(Start:GetChildren()) do
if ( not Class or child:IsA(Class) ) and ( not Tag or child:HasTag(Tag) ) and ( not Name or child.Name == Name ) then
table.insert( List, child )
end
end
return List
end
--[[ Main ]]--
local Doors: {} = FindAll( Services.Workspace, "Model", "Door" )
for id: number, door: Model in pairs(Doors) do
door:SetAttribute( "DoorId", id )
if door:GetAttribute("DoorType") == 2 then
-- Global Variables
local OpenTween: Tween, CloseTween: Tween = Module[door:GetAttribute("DoorType")].GetTweens( door.Door, { Time = 2, EasingStyle = Enum.EasingStyle.Quad, EasingDirection = Enum.EasingDirection.In } )
local RunningThread: thread = nil
local Locked = false
-- Variables
local Busy = false
local Function = Module[door:GetAttribute("DoorType")].DoorFunction
-- Main
local MainThread = coroutine.create(function()
Function(door)
end)
coroutine.resume(MainThread)
-- Cmdr
script.ToggleOpen.Event:Connect(function( ID: number, Open: boolean )
if ID ~= id then
return
elseif Busy then
Instance.new("BoolValue").Parent = script.Parent.Cmdr.Commands.Developer.DoorServer
return
end
Busy = true
if Open then
coroutine.close(RunningThread)
CloseTween:Cancel()
OpenTween:Play()
else
coroutine.close(RunningThread)
OpenTween:Cancel()
CloseTween:Play()
end
Busy = false
end)
script.ToggleLock.Event:Connect(function( ID: number, Lock: boolean )
if ID ~= id then
return
elseif Busy then
Instance.new("BoolValue").Parent = script.Parent.Cmdr.Commands.Developer.DoorServer
return
end
Busy = true
Locked = Lock
Busy = false
end)
script.Fix.Event:Connect(function(ID: number)
if ID ~= id then
return
elseif Busy then
Instance.new("BoolValue").Parent = script.Parent.Cmdr.Commands.Developer.DoorServer
return
end
Busy = true
coroutine.close(RunningThread)
coroutine.close(MainThread)
CloseTween:Play()
Locked = true
RunningThread = nil
MainThread = coroutine.create(function()
Function(door)
end)
coroutine.resume(MainThread)
Busy = false
end)
end
end
Module Script:
local Services = table.freeze {
TweenService = game:GetService("TweenService");
}
local M = {
[2] = {
GetTweens = function( DoorPart: Part, Config: {} ): {Tween}
local Pos = DoorPart.CFrame.Position + (DoorPart.CFrame.RightVector * 12)
local Tween = Services.TweenService:Create( DoorPart, TweenInfo.new( Config.Time, Config.EasingStyle, Config.EasingDirection ), { CFrame=CFrame.lookAt( Pos, (Pos) + DoorPart.CFrame.LookVector, DoorPart.CFrame.UpVector ) } )
Pos = DoorPart.CFrame.Position
return {OpenTween = Tween, CloseTween = Services.TweenService:Create( DoorPart, TweenInfo.new( Config.Time, Config.EasingStyle, Config.EasingDirection ), { CFrame=CFrame.lookAt( Pos, (Pos) + DoorPart.CFrame.LookVector, DoorPart.CFrame.UpVector ) } )}
end,
DoorFunction = function(Door: Model)
print("Door")
-- Init
local Prompt: ProximityPrompt = script.ProximityPrompt:Clone()
Prompt.Parent = Door.Door
-- Main
Prompt.Triggered:Connect(function(Player: Player)
if Locked then
Prompt.ActionText = "Locked"
return
else
Prompt.ActionText = "Open"
end
if typeof(RunningThread) == "thread" then
coroutine.close(RunningThread)
OpenTween:Cancel()
CloseTween:Cancel()
end
RunningThread = coroutine.create(function()
OpenTween:Play()
OpenTween.Completed:Wait()
wait(3)
CloseTween:Play()
end)
coroutine.resume(RunningThread)
end)
end,
}
}
return M