First of all, Hello Scripting Community, i’m here searching some help, again.
→ ¿What do i need?
Well, i’m looking the model, in this case, the Union, dissapears when the same Union is generated again, basically: i just want one union (model/mesh) in the game. Here’s the code that spawns the union 5 studs ahead the character RootPart.
-- Variables
local spawnTool = script.Parent
-- Functions
local function testFunction()
local character = spawnTool.Parent
local charRp = character:WaitForChild("HumanoidRootPart")
local rpCframe = charRp.CFrame
local CFCONFIG = CFrame.new(0, 0, -5)
if charRp then
local hopperModel = game.ServerStorage.redHopper.Union:Clone()
hopperModel.Parent = game.Workspace
hopperModel.Name = "generatedHopper"
hopperModel.Anchored = false
hopperModel.CFrame = rpCframe * CFCONFIG
end
end
-- Conections
spawnTool.Activated:Connect(testFunction)
→ ¿What have i tried?
Tried to use booleans values to evaluate if the model has been spawned, if so, delete it and spawn the new model.
Tried to use another function to check if the model has been spawned.
I can get an idea of how to use the 2 ways above, so, i’m looking if someone can give me some ideas, maybe a little script for example. Anything will be aprecciated! Thx so much (Sorry if is it so confusing, but i seriously need some help).
It took me a while to figure out what you’re trying to do but I think I get it.
Basically you want to destroy the previous model and spawn in a new one everytime the function is called correct? If so here’s how you can do it.
Simply store the model as a variable up top. Heres it being implemented into your code:
-- Variables
local spawnTool = script.Parent
-----------------------------Add this:
local ExistingUnion = nil
-----------------------------
-- Functions
local function testFunction()
-----------------------------Add this:
if ExistingUnion != nil then --Will check that it is not nil and if so then we set it to the last created part
ExistingUnion:Remove() -- Removes the last part
end
-----------------------------
local character = spawnTool.Parent
local charRp = character:WaitForChild("HumanoidRootPart")
local rpCframe = charRp.CFrame
local CFCONFIG = CFrame.new(0, 0, -5)
if charRp then
local hopperModel = game.ServerStorage.redHopper.Union:Clone()
hopperModel.Parent = game.Workspace
hopperModel.Name = "generatedHopper"
hopperModel.Anchored = false
hopperModel.CFrame = rpCframe * CFCONFIG
-----------------------------Add this:
ExistingUnion = hopperModel -- This sets the model as the last existing union
----------------------------- so when you call this func again it ill be removed.
end
end
-- Conections
spawnTool.Activated:Connect(testFunction)
So the if statement just checks if the existing union was set to something meaning the function has passed before and has created a union and stored that union in the existing union variable and if so, it removes the current existing union and continues on to set the new one you are about to create as the new existing union.
One approach to make sure that only one “generatedHopper” exists in the workspace is to use a variable to keep track of whether or not there is already a “generatedHopper” in the workspace. If there isn’t one, we create it as before. If there is one, we simply move it to a new position using the same code that sets its CFrame. Here is an updated version of your code:
-- Variables
local spawnTool = script.Parent
local generatedHopper = nil
local function testFunction()
local character = spawnTool.Parent
local charRp = character:WaitForChild("HumanoidRootPart")
if charRp then
if not generatedHopper then
local hopperModel = game.ServerStorage.redHopper.Union:Clone()
hopperModel.Parent = game.Workspace
hopperModel.Name = "generatedHopper"
hopperModel.Anchored = false
generatedHopper = hopperModel
end
local rpCframe = charRp.CFrame
local CFCONFIG = CFrame.new(0, 0, -5)
generatedHopper.CFrame = rpCframe * CFCONFIG
end
end
spawnTool.Activated:Connect(testFunction)
This should keep track of your “generatedHopper” in the generatedHopper variable and reposition it every time you trigger the spawnTool.