Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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

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.

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

1 comment

0

if there is a script inside the model, it will have an error because you didn't check if v was a basepart

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

This discussion is preserved as it was. New answers, comments, and votes are closed.