So i'm trying to make it so the image will move with the mouse and clone every (0.05) seconds, Iv tried making it clone with a seperate script and nothing is happening I only see my mouse not the clones, What am I doing wrong?
obj = script.Parent while true do script.Parent.Image:Clone() wait(0.05) function updateMove(x, y) script.Parent.Image:Clone() local lx = x - obj.AbsolutePosition.x local ly = y - obj.AbsolutePosition.y obj.Image.Position = UDim2.new(0,lx-150,0,ly-150) end end obj.MouseMoved:connect(updateMove)
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 -- both of these are defaults, where the mouse is before the player puts their mouse over it.
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 -- both of these are defaults, where the mouse is before the player puts their mouse over it. 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 ) -- we're creating a coroutine out of the given function `f` -- c is local so that we can produce multiple coroutine.resume( c ) -- this starts the coroutine end
Next, we'll take the code for making the new image, and put it in its own function:
function makeImage() -- this time it's local, because there will be more than one at once 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 ) -- we're creating a coroutine out of the given function `f` -- c is local so that we can produce multiple coroutine.resume( c ) -- this starts the coroutine end obj = script.Parent originalImage = obj.Image:Clone() mouseX = 0 mouseY = 0 -- both of these are defaults, where the mouse is before the player puts their mouse over it. function makeImage() -- this time it's local, because there will be more than one at once 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