Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How could I make multiple sliding doors (activated by buttons), efficiently?

Asked by 5 years ago

I have a made a sliding door using tweenservice, which works fine,

-- part of server script(remote)
local d1x=d1.Position.X
local d1y=d1.Position.Y
local d1z=d1.Position.Z
local d2x=d2.Position.X
local d2y=d1.Position.Y
local d2z=d2.Position.Z
local newpos1=d1x+2.8
local newpos2=d2x-2.8
root=d1
root2=d2
local d1open = {Position = Vector3.new(newpos1, d1y, d1z)}
local d1close = {Position = Vector3.new(d1x, d1y, d1z)}
local d2open = {Position = Vector3.new(newpos2, d2y, d2z)}
local d2close = {Position = Vector3.new(d2x, d2y, d2z)}
local sound=doors.soundpart.Sound
local grant=doors.soundpart.grant
local deny=doors.soundpart.deny
print(d1open)

I want to replicate this for multiple doors (I am not currently concerned about the fact that they'll only move on the x axis), without having to reference a separate model each time,

-- local script
local repStorage=game:GetService("ReplicatedStorage")
local remote1=repStorage:WaitForChild("Remote1")
local remote2=repStorage:WaitForChild("deny")
local player=game.Players.LocalPlayer
local mouse=player:GetMouse()
local doors=game.Workspace:WaitForChild("doors") -- this is the model for the current door(s)
button=doors.button.ClickDetector
butt=button.Parent
button.MouseHoverEnter:Connect(function()
    button.Parent.SelectionBox.Visible=true
    button.Parent.pressgui.Enabled=true
end)

button.MouseHoverLeave:Connect(function()
    button.Parent.SelectionBox.Visible=false
    button.Parent.pressgui.Enabled=false
end)

local db2=true
local db = false
mouse.Move:Connect(function()
    print(mouse.Target)
    mouse.KeyDown:Connect(function(key)
        if key=='f' then
            if mouse.Target==butt then
                if not db then
                    remote1:FireServer()
                    wait()
                    db = true
                    wait(5)
                    db=false 
                elseif db and db2 then
                    db2=false
                    remote2:FireServer()
                    wait(1.5)
                    db2=true

                end
            end
        end
    end)
end)    

Is there any way that I could somehow either find the parent of a clickdetector that was activated, or anything similar that would not make me have to make separate variables for each model etc. Sorry if I have confused you, please feel free to leave a comment and I'll try to explain it the best I can. Thanks.

1 answer

Log in to vote
0
Answered by
Fifkee 2017 Community Moderator Moderation Voter
5 years ago
Edited 5 years ago

Nice "butt" variable there.

Iterate over all doors by using pairs.

for i, v in pairs(doors:GetChildren()) do
--tween stuff here
end

If you want to make the doors move to the left or right, try using the .RightVector CFrame property. If you multiply the RightVector by a negative value, you will get what I call the "LeftVector." This will also work with LookVector, and this will give you (again, what I call) the BackVector.

Pairs requires a table. Object:GetChildren() gets the children of the object and puts them into a table. With this, we can get the children of "doors" and get all the doors inside of the door folder. Since we can iterate over all door objects using pairs, we don't need to define variables for all objects.

I can't explain "pairs" into words properly, since I'm bad at my own language, but here's what I can say. "i" and "v" are being redefined to the current index/iteration (which is a number in this case, it can be a string or table depending on the key) and the value, hence "i", and "v", standing for "iteration/index" and "value." These values can be changed to whatever you wish.

This then allows us to work in the code block with fresh, new, right out of the oven variables. So, for example, we can do this:


local tab = { egg = true plant = true false, true, false, true workspace.Part; workspace.Eggplant; } for i, v in pairs(tab) do print(i, v) end --[[ egg true plant true 1 false 2 true 3 false 4 true 5 Part 6 Eggplant --]]

Or, with a connection to your example:

local Button = workspace.Part.ClickDetector;
local TS = game:GetService('TweenService');
Button.MouseClick:Connect(function(Click)
    for i, v in pairs(workspace.Doors:GetChildren()) do
        if (v:FindFirstChild('LeftDoor') and v:FindFirstChild('RightDoor')) then --since v is different now and lets say you have different types of doors
            TS:Create(v.LeftDoor, TweenInfo.new(1), {CFrame = CFrame.new(v.LeftDoor.CFrame.rightVector*-5 + v.LeftDoor.CFrame.p)}):Play();
            TS:Create(v.RightDoor, TweenInfo.new(1), {CFrame = CFrame.new(v.LeftDoor.CFrame.rightVector*5 + v.LeftDoor.CFrame.p)}):Play();
        end
    end
    wait(1)
end)
0
Still not sure how I'd do this, or what this would even do, get the children of all the models named 'doors', the single model? Would I put my whole code into this etc? Shrilleh 68 — 5y
0
edited Fifkee 2017 — 5y
Ad

Answer this question