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

Why does this vending machine type script not work?

I'm trying to make it so that when the player presses the button, the surfacegui changes the text, then it spits out the item the player requested, all of the surfacegui stuff works except for the part where it's supposed to give the player the part.

local cd = script.Parent.ClickDetector
local TextLabel = game.Workspace.MainScreen:WaitForChild("GuiPart"):WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local Model = game.ReplicatedStorage.Model

cd.MouseClick:Connect(function()
	wait(1)
	TextLabel.Text = "STARTING PROCESS..."
	wait(5)
	TextLabel.Text = "REQUESTED ITEM EJECTED, THANK YOU."
	wait(1)
	TextLabel.Text = "PLEASE PUSH BUTTON TO GET STARTED"
	
	wait(6)
		local Clone = Model:Clone()
		for _, part in pairs(Clone:GetChildren()) do
			
			part.Parent = workspace
			
			part.Position = Vector3.new(54.62, 4.17, 31.68)
		end
end)

1 comment

0

why are you setting the position of the parts manually.. You could just set it to another part's position. So wherever you place it in the game world it would still work.

1 answer

You do not have to loop through the children of the model. All you have to do is set a primary part to your model like this: model.PrimaryPart = part . Next you simply have to use the :MoveTo function to move the model to your desired Vector3 position. To make it so the vending machine can work any where in the game word, simply move the model to an invisible part which is where you want the snack to appear. If you don't have one already, create it and place it then name it "SnacksPositionPart"

Here's how your script should look like:

local cd = script.Parent.ClickDetector
local TextLabel = game.Workspace.MainScreen:WaitForChild("GuiPart"):WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local model= game.ReplicatedStorage.Model
-- Setting a primary part to the model which is a child of the model (choose a part).
model.PrimaryPart = model.RANDOMPART
-- Getting the invisible part which is position where you want your snacks to appear
 local snacksPositionPart = newModel:FindFirstChild("SnacksPositionPart")
cd.MouseClick:Connect(function()
    wait(1)
    TextLabel.Text = "STARTING PROCESS..."
    wait(5)
    TextLabel.Text = "REQUESTED ITEM EJECTED, THANK YOU."
    wait(1)
    TextLabel.Text = "PLEASE PUSH BUTTON TO GET STARTED"

    wait(6)
        local newModel = model:Clone()
        newModel:MoveTo(snacksPositionPart.Position)
	newModel.Parent = game.Workspace
end)

1 comment

0

Have you tried it? Has it worked? Let me know.

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