Hello, I’ve been working on a game for some time now where you essentially cook fast foods like chips/french fries and chicken. The Issue I am having is the things like the fryer.
At the moment I have set it up so that when a part touches the fryer it waits 5 seconds before cloning a part to change the color of the food put into it. Afterwards the cloned script destroys itself. Occasionally it doesn’t destroy itself and just stays in the workspace. The script I am using is:
local touchpart3 = game.Lighting.touchpart3
function onTouched(part)
touchpart3:Clone().Parent = game.Workspace
wait(6)
game.Workspace.touchpart3:remove()
end
connection = bin.Touched:connect(onTouched)
I’ve tried changing things around, but it doesnt seem to always work. Would there be another way were you can place a part into a fryer for say 5 seconds and it would change color?
3 Likes
I think this is because now you have two things in the Workspace called touchpart3, and the script doesn’t know which one to destroy.
1 Like
First, some notes (that won’t fix things but is better practice):
- Use
ReplicatedStorage
to store parts, not Lighting
- Use
:Destroy()
instead of :Remove()
Touched
is going to fire a lot of times, so you’ll end up with something like
Fries touched, create workspace.touchpart3
Fries touched, create workspace.touchpart3
Fries touched, create workspace.touchpart3
... wait 6 seconds ...
Remove workspace.touchpart3 (but which one??)
Remove workspace.touchpart3 (but which one??)
Remove workspace.touchpart3 (but which one??)
Also, I’m generally confused about what your script does. You’re making a new part and then deleting it later? If you were trying to swap one part with another, wouldn’t you want to wait 6 seconds, then create the touchpart3
clone and delete part
at the same time?
1 Like
local touchpart3 = game.Lighting.touchpart3
function onTouched(part)
local clone = touchpart3:Clone()
clone.Parent = game.Workspace
wait(6)
clone:Destroy()
end
connection = bin.Touched:connect(onTouched)
1 Like
Basically what I need it to be something like this, but this doesn’t seem to work so that is why I probably over complicated it a lot.
bin = script.Parent
function onTouched(part)
wait(5)
workspace.values.Value = ‘true’
end
if workspace.values.Value == ‘true’ then
workspace.values.Value = ‘false’
wait()
function onTouched(part)
part.BrickColor = script.Parent.BrickColor
end
end
connection = bin.Touched:connect(onTouched)
what I am trying to make the script do is to change the color of a part after it has been touching for five seconds, what this should supposedly do is after 5 seconds it checks to see if the part is still touching and if its not then the part wont change color but if it is still touching then it will.
I wasn’t quite sure how to go about this so I ended up having different scripts with delays being cloned into the workspace which isn’t very efficient.
This worked for me. When I walk into the part (make sure the part is anchored with can collide off), if I stay there for 5 seconds my skin turns to the part’s color. Also works if you drop an object inside of the part.
changedTable = {}
script.Parent.Touched:Connect(function(hit)
if hit:IsA("BasePart") and string.lower(hit.Name) ~= "baseplate" and not table.find(changedTable, hit) then
print("insert", hit)
table.insert(changedTable, hit)
wait(2)
for i, v in pairs(script.Parent:GetTouchingParts()) do
print(i, v.Name)
if v == hit then
hit.Color = script.Parent.Color
print("changing")
break
end
end
end
end)
script.Parent.TouchEnded:Connect(function(hit)
if table.find(changedTable, hit) then
table.remove(changedTable, table.find(changedTable, hit))
end
end)
1 Like