Why isn't this working?

I made a script that’s supposed to combine two parts, it doesn’t seem to be working and there are no errors.

local object = script.Parent
local objectLabel = object:WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local stateOfMatter = object:GetAttribute("StateOfMatter")
local MoveModule = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("ModuleScript"))

while task.wait(2) do
	MoveModule:Move(object, stateOfMatter)
end

object.Touched:Connect(function(hit)
	print(hit:GetAttribute("Element"))
	if hit:GetAttribute("Element") == "Hydrogen" and hit:GetAttribute("Atoms") == 2 and hit ~= nil then
		hit:Destroy()
		object.Name = "water"
		object.BrickColor = BrickColor.new("Storm blue")
		objectLabel.Text = "H₂O"
	end
end)

while task.wait(2) do
	MoveModule:Move(object, stateOfMatter)
end
1 Like

object, objectLabel, and stateOfMatter are only checked once when the script loads. This is almost certainly not what you want, since you use those values any time the Touched event fires. Also it isn’t possible for anyone to evaluate what the Touched function should do without knowledge of what your attributes mean.

Element is a string Atoms is a int value

also, I’m not sure why I would have to update object and object label, as its just a part and a textlabel :thinking:

Can you stop coding for a bit and write a descriptive document of what your end goal is with this system? It is going to be very hard for people to help otherwise. I vaguely know what you’re trying to do because I’ve seen several of your questions in a row, but other people do not, and theres a good chance you will understand it a lot better if you have a design document to work from.

Try adding prints to see if the script succeeds with parts.
Also which line of code seems to be broken?

your main issue maybe that your script is probably never getting past this while loop you either need to wrap it or remove it

really if this is not printing on touch – use more prints to test your code make sure its getting to the bottom

My end goal for this system is to make a system so that 2 or more parts that represent elements can combine to make a new compound.

I’m not sure what else to say, sorry that I cannot make it the upmost descriptive. If you want to have a description about what I’m doing with my code, I can provide you that.

I’ve tried re editing the code and it doesn’t make much of a difference. I still seem to get the same outcome.

local object = script.Parent
local objectLabel = object:WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local MoveModule = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("ModuleScript"))

object.Touched:Connect(function(hit)
	print(hit:GetAttribute("Element"))
	if hit:GetAttribute("Element") == "Hydrogen" and hit:GetAttribute("Atoms") == 2 and hit ~= nil then
		hit:Destroy()
		object.Name = "water"
		object.BrickColor = BrickColor.new("Storm blue")
		objectLabel.Text = "H₂O"
	end
end)

while task.wait(2) do
	MoveModule:Move(object)
end

If you add comments to your code such that they explain what you are expecting each line to do, it will be easy to answer if your expectations are correct and how to fix the code if they are not. Code is only as useful as the documentation it embodies.

is the touched even printing this or not?

also try doing this to see if your loop is even running or if your module script is yielding it

local object = script.Parent
local objectLabel = object:WaitForChild("SurfaceGui"):WaitForChild("TextLabel")
local MoveModule = require(game:GetService("ServerScriptService"):WaitForChild("Modules"):WaitForChild("ModuleScript"))

object.Touched:Connect(function(hit) -- when the object is hit, begin the process of checks
	print(hit:GetAttribute("Element")) -- check the element of hit
	if hit:GetAttribute("Element") == "Hydrogen" and hit:GetAttribute("Atoms") == 2 and hit ~= nil then
		hit:Destroy() -- destroy the hit part so that we can change this one into the new element/compound
		object.Name = "water"
		object.BrickColor = BrickColor.new("Storm blue")
		objectLabel.Text = "H₂O"
	end
end)

while task.wait(2) do -- <--- works
	MoveModule:Move(object)
end

its working

ok so it is all working or it just prints running loop?

ok so what art is not working now?

any errors on touch and your check for hit should be at the begining not end of the if statement

could do it like this


object.Touched:Connect(function(hit)
	print(hit:GetAttribute("Element"))
	if hit and hit.Parent and hit:GetAttribute("Element") == "Hydrogen" and hit:GetAttribute("Atoms") == 2 then
		hit:Destroy()
		object.Name = "water"
		object.BrickColor = BrickColor.new("Storm blue")
		objectLabel.Text = "H₂O"
	end
end)
1 Like