Function storing previous data

Just a disclaimer before I start; I have created a post on this before and I deleted it because I realized that what I said was incorrect. Sorry about this, but please forgive me!

I am creating a project where you can create custom explosives. To do this you are given a tool which opens a GUI when you click an explosive part with it. You can insert different values into this GUI which can alter how powerful the explosion created by the part is.

The issue is that it appears that when you save/cancel your changes, the functions seem to store the data from previous parts you’ve interacted with? Here is an example:

Changing name of “Explosive” to “test1”

Click confirm and…

image

Perfect! Works brilliantly, the name of the part has been changed. Now, let’s change the name of “Explosive two” to “test2”

Click confirm aaannndd…

image

Brilliant. It has changed the name of “test1” to “test2” AND “Explosive two” to “test2”. Keep in mind I ONLY wanted “Explosive two” to change name. This is the same thing for ALL my settings I can change on these parts using my tool. Same would happen if I were to change “Explosive three” to “test3”. All parts would change to “test3”.

Here’s the local script contained within the tool:

wait(1)

local player = game:GetService("Players").LocalPlayer

local mouse = player:GetMouse()

local sp = script.Parent

local gui = player.PlayerGui.ScreenGui

local event = game:GetService("ReplicatedStorage").RemoteEvent

sp.Equipped:Connect(function(mouse)
	mouse.Button1Up:Connect(function()
		if mouse.Target then
			local  target = mouse.Target
			if target:GetAttribute("explosive") == true then
				gui.Enabled = true
				gui.Frame.densityval.Text = target.CurrentPhysicalProperties.Density
				gui.Frame.material.Text = target:GetAttribute("material")
				gui.Frame.partname.Text = target.Name
				gui.Frame.explode:SetAttribute("destroy", target:GetAttribute("destroy"))

				
				local function close()
					gui.Enabled = false
					print(target) --[1]
				end
				
				local function save()
					gui.Enabled = false
					target.CustomPhysicalProperties = PhysicalProperties.new(tonumber(gui.Frame.densityval.Text), target.CurrentPhysicalProperties.Elasticity, target.CurrentPhysicalProperties.ElasticityWeight,target.CurrentPhysicalProperties.Friction,target.CurrentPhysicalProperties.FrictionWeight)
					target:SetAttribute("material",gui.Frame.material.Text)
					target.Name = gui.Frame.partname.Text
					target:SetAttribute("destroy", gui.Frame.explode:GetAttribute("destroy"))
					event:FireServer(target.CurrentPhysicalProperties.Density, target.CurrentPhysicalProperties.Elasticity, target.CurrentPhysicalProperties.ElasticityWeight, target.CurrentPhysicalProperties.Friction, target.CurrentPhysicalProperties.FrictionWeight, target:GetAttribute("material"), target.Name, target, target:GetAttribute("destroy"))
					print(target) --[2]
				end
				
				
				gui.Frame.cancel.Activated:Connect(close)
				gui.Frame.confirm.Activated:Connect(save)
				
			end
		end
	end)
end)

At [1] and [2] this seems to be where the script may be choking up. Let’s say you wanted to change the name of “Explosive” to “test1” just like the example shown above, this is your printed:

Explosive

If you then changed “Explosive two” to “test2”, this is your result:

Explosive
Explosive two
Explosive

This is not in a list. These are all separate outputs. If you then changed “Explosive three” to “test3”:

Explosive
Explosive two
Explosive
Explosive three
Explosive two
Explosive

I will say again, what I am looking for is when I change name of “Explosive” to “test1” I want it to rename to “test1”. When I change name of “Explosive two” to “test2” I ONLY want “Explosive two” to change name to “test2”, not “Explosive”. Same for “Explosive three” to “test3”, I ONLY want “Explosive three” to change name to “test3”, not all of the other previously selected parts.

I haven’t attempted any solutions because I do not know how to approach it. Nothing really seems wrong with my script to me… any help is appreciated!

From the looks of your code, I believe the problem is that you never disconnect the function inside the equip function whenever you equip your tool. The same goes with your “Activated” events, those connections continue to pile up each time you connect them. You must disconnect them once they are finished being used.

1 Like

Ah, I’ve never heard of this before as I am a relatively noob scripter, haha! I will have a look into this and update you on how I get on!

1 Like

I have fixed it and it works a dream! All I did was disconnect the save and close connections as I wasn’t sure how to do the mouse one but it appears to have worked fine. Hope I haven’t spoken too soon on that one… Thank you though!

2 Likes

Also, another good thing to do would be to put the functions outside of connections or other inside other functions unless necessary. But yeah, if you don’t mind posting @Dragonfable6000’s reply as solved, it’d be nice.

Oh yeah, forgot about marking as solved. Thank you!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.