decide to get back into coding, i still remember most basic stuff but im stumped at for i, v’s
the idea for the script is that multiple gui buttons are assigned a tag called “fumo-button” (rather than putting the same code with only the model name changed repeatedly)
where if you click it a model on the workspace named on the “listo” table will be destroyed and replaced by a new one written on the string attribute “thefum” stored on the frame gui (for example : the fum = “Mokou”).
i settled on this and left stuck due to the script not making any error on the output, any help would be appreciated ^
local collection = game:GetService("CollectionService")
local storage = game.ServerStorage
local folder = storage:WaitForChild("Fumo")
local listo = {"Impostor", "Cirno", "Flandre", "Pelo:0", "Reimu", "Remilia", "InuSakuya", "Yuuka", "Youmu", "Yuyuko", "Mokou", "Shion"}
for i, model in pairs(collection:GetTagged("fumo-button")) do
local att = model:GetAttribute("thefum")
local fumo = folder:WaitForChild(att)
local function dodo()
for i, v in pairs(workspace:GetChildren()) do
if table.find(listo, v.Name) then
task.spawn(function()
v:Destroy()
task.wait()
local clone = fumo:Clone()
clone.Parent = workspace
end)
break
end
end
end
model.Button.MouseButton1Click:Connect(function()
dodo()
end)
end
So, you want to delete a model when a button is pressed… and a new model should replace it which is stated in the attribute?
Does the order of models being destroyed matter? because otherwise you can randomize it.
But anyway, is there any output? you said no errors, but any warnings maybe?
Because the only thing I can see going wrong is maybe a waitforchild call did an infinite yield?
also, it pains me to see this:
--you're not using dodo anywhere else so just put it in the event..
model.Button.MouseButton1Click:Connect(function()
for _, v in pairs(workspace:GetChildren()) do
if table.find(listo, v.Name) then
task.spawn(function()
v:Destroy()
task.wait()
local clone = fumo:Clone()
clone.Parent = workspace
end)
break
end
end
end)
you can place some print statements in your code to see where your program stops and what it actually executes.
Just put some prints maybe in each loop and in the function to see where it stops and maybe report back if you want…
Hope this helps, but I don’t think this gave you many insights.
edit: maybe you shouldn’t destroy models on the client, add a remote event call
Ok so the main issue I see is that you’re not checking against anything except for the existence of current model’s name being in your list.
To add on to that as an issue, if you find a model that has a name within your list, it immediately breaks the loop, ending that execution. You will not find any errors with that.
If you have multiple of these models, they will very likely interfere with each other.
for i, v in pairs(workspace:GetChildren()) do
if table.find(listo, v.Name) then
Also, before asking for assistance (although we’re happy to help) always try debug statements first. Add prints, ensure code is reaching certain points, print out variables—you can always print i,v and find out where it’s stopping or breaking—as a sanity check for yourself, so you don’t get overly frustrated.
Quick Edit: @scavengingbot and he is not deleting models on the Client. The code he has sent has ServerStorage defined within the first 3 lines.
OP, you’re subscribing to client user input from the server! that’s why I thought it was on the client, button events are client sided, so you need to put this in a local script with remote events to destroy your models!
wait what, I just tested it and in my entire career I’ve never realized that…
Wow, okay thank you, I have tested it both with the script parented under the button and with collection service as seen here, and it doesn’t work with col service but it does work with script as a child of the button.
Now maybe it is because when gettagged is called the button doesn’t exist, and since the button loads first and then the script when the script is a child, the button is loaded when the script runs. so I made a test script, without col:getinstanceaddedsignal and with it, the first one failed but the latter worked so just add getinstanceaddedsignal to your script to make sure the buttons get an event even if they load a bit after the script runs. Like so (test script):
local col = game:GetService("CollectionService")
local function h(ins)
ins.MouseButton1Click:Connect(function()
print("hello world!!!!!!")
end)
end
col:GetInstanceAddedSignal("cc"):Connect(h)
col:GetInstanceRemovedSignal("cc"):Connect(h)
for _, nnsnsn in ipairs(col:GetTagged("cc")) do
h(nnsnsn)
end
--this works, ignore the weird names, I made them up in 1s.
--also .Activated doesn't work so don't use that event.
hope this helps I guess?
edit: I forgot to delete the ‘reply’ marker, I wanted to @ you instead, now the OP won’t get a notification on this.
it worked! thank you for the help both @IDoLua and @scavengingbot!
and sorry for the late response, different timezone.
i forgot to mention the script was on serverscript rather than inside the gui, which fixed the buttons not doing anything, thanks for that
anyways here is my final script, let me know if there’s something i need to change :7
local collection = game:GetService("CollectionService")
local storage = game.ServerStorage
local folder = storage:WaitForChild("Fumo")
local listo = {"Impostor", "Cirno", "Flandre", "Pelo:0", "Reimu", "Remilia", "InuSakuya", "Yuuka", "Youmu", "Yuyuko", "Mokou", "Shion"}
local function zebutton(ins)
local att = ins:GetAttribute("thefum")
local fumo = folder:WaitForChild(att)
ins.Button.MouseButton1Click:Connect(function()
for i, v in pairs(workspace:GetChildren()) do
if table.find(listo, v.Name) then
task.spawn(function()
v:Destroy()
task.wait()
local clone = fumo:Clone()
clone.Parent = workspace
end)
break
end
end
end)
end
collection:GetInstanceAddedSignal("fumo-button"):Connect(zebutton)
collection:GetInstanceRemovedSignal("fumo-button"):Connect(zebutton)
for i, model in pairs(collection:GetTagged("fumo-button")) do
zebutton(model)
end