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
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.
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.
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.
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
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)