You've misunderstood how mouse move works and how connections work.
Never define a function inside of a loop. There are very rare circumstance where you want to do this, but most of the time you can fix this.
Your start is fine:
obj = script.Parent
We're also going to create an original image copy:
originalImage = obj.Image:Clone()
But then you don't want the loop. What we'll do is we'll keep track of where the mouse is, by defining new variables:
mouseX = 0
mouseY = 0
Next, we're going to use the update function to change these to reflect the position of the mouse:
function updateMove( x, y)
mouseX = x
mouseY = y
end
This creates a function called updateMove that takes two arguments, x and y. All it does is set the variables mouseX and mouseY to be copies of these.
But on its own, nothing happens. It's just a block of code to be called later. So it won't run yet. We add the connection:
obj.MouseMoved:connect( updateMove )
Now, whenever the mouse is moved, updateMove is called with the new mouse position. So now mouseX and mouseY will always reflect the current mouse position.
Now we do the loop.
while true do
wait(0.05)
Next we create the clone. What you did clones the object, but its parent remains nil so it isn't added anywhere. Also, it will be inconsistent potentially since you don't refer to the specific one.
So we're going to clone the label and create a reference to it.
img = originalImage:Clone()
img.Position = UDim2.new( 0, mouseX - obj.AbsolutePosition.x - 150 , 0, mouseY - obj.AbsolutePosition.y - 150)
img.Parent = obj
And then just end the while-loop:
end
And there you go. This should functional like what you expect (though I'm not exactly sure what you expect from this).
All together:
obj = script.Parent
originalImage = obj.Image:Clone()
mouseX = 0
mouseY = 0
function updateMove( x, y)
mouseX = x
mouseY = y
end
obj.MouseMoved:connect( updateMove )
while true do
wait(0.05)
img = originalImage:Clone()
img.Position = UDim2.new( 0, mouseX - obj.AbsolutePosition.x - 150 , 0, mouseY - obj.AbsolutePosition.y - 150)
img.Parent = obj
end
EDIT: If you want them to disappear after some time, there are a couple ways to do this. Coroutines are one solution. There are other solutions, but they aren't as neat.
We're going to need some new functions.
function async( f )
local c = coroutine.create( f )
coroutine.resume( c )
end
Next, we'll take the code for making the new image, and put it in its own function:
function makeImage()
local img = originalImage:Clone()
img.Position = UDim2.new( 0, mouseX - obj.AbsolutePosition.x - 150 , 0, mouseY - obj.AbsolutePosition.y - 150)
img.Parent = obj
wait( 15 )
img:Remove()
end
We want it to run async, so we'll replace that code in the loop with async( makeImage )
All together:
function async( f )
local c = coroutine.create( f )
coroutine.resume( c )
end
obj = script.Parent
originalImage = obj.Image:Clone()
mouseX = 0
mouseY = 0
function makeImage()
local img = originalImage:Clone()
img.Position = UDim2.new( 0, mouseX - obj.AbsolutePosition.x - 150 , 0, mouseY - obj.AbsolutePosition.y - 150)
img.Parent = obj
wait( 15 )
img:Remove()
end
function updateMove( x, y)
mouseX = x
mouseY = y
end
obj.MouseMoved:connect( updateMove )
while true do
wait(0.05)
async( makeImage )
end
3 comments
Thanks so much! and I was using it for this: http://www.roblox.com/NICHOLAS-CAGE-IS-LOVE-NICHOLAS-CAGE-IS-LIFE-place?id=32950531
Is there anyway to delete the clones after 15 seconds?
There are several ways you could do it, but they require some changes. I will edit my answer. You should edit your question, too.