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

How to change properties of a model with a script when a button is clicked?

Asked by 4 years ago

I am trying to make a gate that opens for 5 seconds. I have two models, one is visible and stays on the ground "closedgate" and there is another one that is floating above it and is usually invisible "opengate" I'm trying to change the properties of the parts inside of the models so there is an effect of the door opening when a button is pressed.

This is the code I have currently.

local button = script.Parent
local closedgate = workspace.ClosedGate:GetChildren() --gets children of closed gate model
local opengate = workspace.OpenGate:GetChildren() --gets children of open gate model

local function openGate()
    print("Click Detected")
    closedgate.Transparency = 1 --makes the gate invisible
    closedgate.CanCollide = false --makes player able to walk through gate
    opengate.Transparency = 0 --makes floating gate visible
    print("Gate Opened")
    wait(5)
    closedgate.Transparency = 0 --makes gate visible
    closedgate.CanCollide = true --makes player unable to walk through gate
    opengate.Transparency = 1 --makes floating gate invisible
    print("Gate Closed")
end

button.ClickDetector.MouseClick:Connect(openGate) --runs opengate function when clicked

3 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

you have to iterate through the children of your models, for example:

for i, v in pairs(workspace.ClosedGate:GetChildren()) do
    v.Transparency = 0
end

v = what ever part the for loop is currently iterating through. note this will set every parts transparency to 0 so i suggest you check for names.

Ad
Log in to vote
0
Answered by 4 years ago

You can use :GetChildren To get the Entire Parts in that Model, And use i,v pairs to loop into the model Until the loop reach Descendants, of the Model Your targeting.

Learn Loops

Understanding the loops personality

local button = script.Parent
local closedgate = workspace.ClosedGate:GetChildren() --gets children of closed gate model
local opengate = workspace.OpenGate:GetChildren() --gets children of open gate model
local TimeDelay = 5


function openGate()
    for i, v in pairs(opengate) do
        v.Transparency = 0
        v.CanCollide = true
    end
    for i, v in pairs(closedgate) do
        v.Transparency = 1
        v.CanCollide = false
    end


    print("Gate Opened")
    wait(TimeDelay)


    for i, v in pairs(opengate) do
        v.Transparency = 1
        v.CanCollide = false
    end
    for i, v in pairs(closedgate) do
        v.Transparency = 0
        v.CanCollide = true
    end
    print("Gate Closed")
end

button.ClickDetector.MouseClick:Connect(openGate) --runs opengate function when clicked
0
if there is a script inside the model, it will have an error because you didn't check if v was a basepart AnasBahauddin1978 715 — 4y
Log in to vote
0
Answered by 4 years ago

What you are doing is you are changing the transparency of the model which doesn't exist, you can try looping through the model to make them transparent:

local button = script.Parent
local closedgate = workspace.ClosedGate:GetChildren() --gets children of closed gate model
local opengate = workspace.OpenGate:GetChildren() --gets children of open gate model

local function openGate()
    print("Click Detected")
    for _,v in pairs(opengate) do
        if v:IsA("BasePart") then -- If v(Parts inside the model) are parts or meshparts then
            v.Transparency = 1
            v.CanCollide = false
        end
    end
    for _,v in pairs(closedgate) do
        if v:IsA("BasePart") then -- Same thing
            v.Transparency = 0
            v.CanCollide = true
        end
    end
end
button.ClickDetector.MouseClick:Connect(openGate) --runs opengate function when clicked

Answer this question