I’m making a small project where the player is looking at a plate, and when they click the keys E, R, F, C, and X, they spawn in from the sky parts of a burger, like cheese, tomatos, and bread, each corresponding to a certain key. Once there is 6 ingredients added, then the burger and the plate slide(tween) to the left and are destroyed. A few seconds later a new plate slides from the right to in front of the player.
It works perfectly fine the first time. However, the second time around it stops working and glitches. The plate doesn’t slide to the left and a few more rounds of placing the ingredients just breaks it.
I’ve looked around but couldn’t really find any solutions, and I spent countless hours trying to fix it. Such as renaming the newPlate to plate because maybe it couldn’t find it, and moving the entire module script into the same Local script because the point was to test module scripts and what if I understood it wrong?
Anyways, I would really appreciate some help on this topic. The scripts are below, and here is a video to demonstrate.
game file: burgerissue.rbxl (64.1 KB)
Local Script
Located in StarterPlayerScripts
local uis = game:GetService("UserInputService")
local burgerTable = require(game.ReplicatedStorage.BurgerModuleScript)
local counter = burgerTable.ingredientCounter
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E and counter < 6 then
--print("placing bread")
counter = counter + 1
burgerTable.cloneBread()
else return
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.R and counter < 6 then
print("placing cheese")
counter = counter + 1
burgerTable.cloneCheese()
else return
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.F and counter < 6 then
print("placing meat")
counter = counter + 1
burgerTable.cloneMeat()
else return
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.C and counter < 6 then
print("placing tomato")
counter = counter + 1
burgerTable.cloneTomato()
else return
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.X and counter < 6 then
print("placing lettuce")
counter = counter + 1
burgerTable.cloneLettuce()
else return
end
end)
uis.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.X or input.KeyCode == Enum.KeyCode.C or input.KeyCode == Enum.KeyCode.F or input.KeyCode == Enum.KeyCode.R or input.KeyCode == Enum.KeyCode.E then
if counter == 5 then
print("cannot create more bread")
wait(1.5)
-- add error function here and finished
-- make wall cancollide false
burgerTable.burgerDone()
counter = 0
end
else return
end
end)
Module Script
Located in Replicated Storage
local module = {
-- burger
bread = game.Workspace:WaitForChild("bread"),
cheese = game.Workspace:WaitForChild("cheese"),
meat = game.Workspace:WaitForChild("meat"),
tomato = game.Workspace:WaitForChild("tomato"),
lettuce = game.Workspace:WaitForChild("lettuce"),
plate = game.Workspace:WaitForChild("plate"),
cloneLocation = game.Workspace:WaitForChild("cloneLocation"),
ingredientCounter = 0,
itemTable = {},
}
module.cloneBread = function()
local newBread = module.bread:Clone()
local cloneLocation = module.cloneLocation
local ingredientCounter = module.ingredientCounter
local itemTable = module.itemTable
table.insert(itemTable, newBread)
newBread.Parent = game.Workspace
newBread.CFrame = cloneLocation.CFrame * CFrame.new(0,20,0)
newBread.CFrame = newBread.CFrame * CFrame.Angles(0,0,0)
end
module.cloneCheese = function()
local newCheese = module.cheese:Clone()
local cloneLocation = module.cloneLocation
local ingredientCounter = module.ingredientCounter
local itemTable = module.itemTable
table.insert(itemTable, newCheese)
newCheese.Parent = game.Workspace
newCheese.CFrame = cloneLocation.CFrame * CFrame.new(0,20,0)
newCheese.CFrame = newCheese.CFrame * CFrame.Angles(0,0,0)
end
module.cloneMeat = function()
local newMeat = module.meat:Clone()
local cloneLocation = module.cloneLocation
local ingredientCounter = module.ingredientCounter
local itemTable = module.itemTable
table.insert(itemTable, newMeat)
newMeat.Parent = game.Workspace
newMeat.CFrame = cloneLocation.CFrame * CFrame.new(0,20,0)
newMeat.CFrame = newMeat.CFrame * CFrame.Angles(0,0,0)
end
module.cloneTomato = function()
local newTomato = module.tomato:Clone()
local cloneLocation = module.cloneLocation
local ingredientCounter = module.ingredientCounter
local itemTable = module.itemTable
table.insert(itemTable, newTomato)
newTomato.Parent = game.Workspace
newTomato.CFrame = cloneLocation.CFrame * CFrame.new(0,20,0)
newTomato.CFrame = newTomato.CFrame * CFrame.Angles(0,0,0)
end
module.cloneLettuce = function()
local newLettuce = module.lettuce:Clone()
local cloneLocation = module.cloneLocation
local ingredientCounter = module.ingredientCounter
local itemTable = module.itemTable
table.insert(itemTable, newLettuce)
newLettuce.Parent = game.Workspace
newLettuce.CFrame = cloneLocation.CFrame * CFrame.new(0,20,0)
newLettuce.CFrame = newLettuce.CFrame * CFrame.Angles(0,0,0)
end
module.burgerDone = function()
local ts = game:GetService("TweenService")
local itemTable = module.itemTable
local newPlate = module.plate:Clone()
local oldPlate = module.plate
local itemOne = itemTable[1]
local itemTwo = itemTable[2]
local itemThree = itemTable[3]
local itemFour = itemTable[4]
local itemFive = itemTable[5]
local itemSix = itemTable[6]
local itemSeven = itemTable[7]
local weld1 = Instance.new("WeldConstraint")
local weld2 = Instance.new("WeldConstraint")
local weld3 = Instance.new("WeldConstraint")
local weld4 = Instance.new("WeldConstraint")
local weld5 = Instance.new("WeldConstraint")
local weld6 = Instance.new("WeldConstraint")
oldPlate.Name = "oldPlate"
newPlate.Name = "plate"
newPlate.Transparency = 1
newPlate.Parent = game.Workspace
print(oldPlate.Name)
print(newPlate.Name)
weld1.Parent = oldPlate
weld2.Parent = oldPlate
weld3.Parent = oldPlate
weld4.Parent = oldPlate
weld5.Parent= oldPlate
weld6.Parent = oldPlate
weld1.Part0 = oldPlate
weld1.Part1 = itemOne
weld2.Part0 = oldPlate
weld2.Part1 = itemTwo
weld3.Part0 = oldPlate
weld3.Part1 = itemThree
weld4.Part0 = oldPlate
weld4.Part1 = itemFour
weld5.Part0 = oldPlate
weld5.Part1 = itemFive
weld6.Part0 = oldPlate
weld6.Part1 = itemSix
local tweeninfoaway = TweenInfo.new(.4, Enum.EasingStyle.Linear, Enum.EasingDirection.In)
local tweeninfonew = TweenInfo.new(.5, Enum.EasingStyle.Sine, Enum.EasingDirection.In)
local goalsAway = {
CFrame = oldPlate.CFrame * CFrame.new(0,0,-50);
}
local goalsNew = {
CFrame = CFrame.new(-28.5, 0.5, 35, 0, 0, 1, 1, 0, 0, 0, 1, 0 )
}
local tweenAway = ts:Create(oldPlate, tweeninfoaway, goalsAway)
local tweenNew = ts:Create(newPlate, tweeninfonew, goalsNew)
tweenAway:Play()
wait(1.5)
itemTable[1]:Destroy()
itemTable[2]:Destroy()
itemTable[3]:Destroy()
itemTable[4]:Destroy()
itemTable[5]:Destroy()
itemTable[6]:Destroy()
newPlate.Transparency = 0
newPlate.CFrame = newPlate.CFrame * CFrame.new(0,0,50)
tweenNew:Play()
newPlate.Name = "plate"
table.clear(itemTable)
oldPlate:Destroy()
end
return module