Setting gui's position as mouse cursor position

So I have been trying to set the gui textlabel’s position as my cursor’s position

cloneHealth.TextLabel.Position = UDim2.new(0, cursor.Hit.p.X , 0, cursor.Hit.p.Y)

I saw people trying to do this but their solution doesn’t seem to work for me ( idk why ), I need help!!! Thanks

1 Like

try

cloneHealth.TextLabel.Position = UDim2.new(0, cursor.X , 0, cursor.Y)
1 Like

make sure cursor = game.Players.LocalPlayer:GetMouse()

it still doesn’t work for some reason, the gui 's position IS different, but its still not what I wanted it to be ( at the cursor’s position )

but is it moving when u are moving the mouse?

the thing is that u can add something to cursor.X and cursor.Y like this:

here is the link of the screen recording

did u update the textlabels position only once?
if yes u have to repeat the process unlimited times:

while true do
    wait(0.1)
    cloneHealth.TextLabel.Position = UDim2.new(0, cursor.X - 200 , 0, cursor.Y - 50)
end

u can edit those numbers i put after the x and y

do you have any idea on how to put the loop inside this since if I put a forever loop it won’t stop

cursor.Move:Connect(function()
	if item then
		if item.Parent:FindFirstChild("Enemy") then
			
			local cloneHealth = game.ReplicatedStorage.ShowHealth:Clone()
			cloneHealth.Parent = player.PlayerGui
			local currentHealth = item.Parent.Health
			--print(cursor.Hit.p.X, cursor.Hit.p.Y)
			cloneHealth.TextLabel.Text = ((item.Parent.Health.Value).." / "..(currentHealth.Value))
			
		
			
		end
	end
end)

just add

cloneHealth.TextLabel.Position = UDim2.new(0, cursor.X , 0, cursor.Y)

there

umm I added that at first and it doesn’t work…

i have done tower defense game before and u do this very weird way ur even cloning the gui.
Can u show me the whole gui thingy and i maybe can do the whole script

and also where are the enemies parent. Is it some kind of folder in workspace?

It is in a folder called Enemies in workspace

here:

local userInputService = game:GetService("UserInputService")
local camera = game.Workspace.CurrentCamera
local runService = game:GetService("RunService")
local player = game.Players.LocalPlayer

local cursor = player:GetMouse()

function mouseRayCast()
	local mousePos = userInputService:GetMouseLocation()

	local mouseRay = camera:ViewportPointToRay(mousePos.X +2, mousePos.Y)

	local raycastResult = game.Workspace:Raycast(mouseRay.Origin, mouseRay.Direction * 1000)

	return raycastResult
end

runService.RenderStepped:Connect(function()
	local result = mouseRayCast()
	if result and result.Instance then
		item = result.Instance
	end
end)

player.PlayerGui.ChildAdded:Connect(function(child)
	if child.Name == "ShowHealth" then
		
		task.wait(0.05)
		child:Destroy()
	end
end)

cursor.Move:Connect(function()
	if item then
		if item.Parent:FindFirstChild("Enemy") then
			
			local cloneHealth = game.ReplicatedStorage.ShowHealth:Clone()
			cloneHealth.Parent = player.PlayerGui
			local currentHealth = item.Parent.Health
			--print(cursor.Hit.p.X, cursor.Hit.p.Y)
			cloneHealth.TextLabel.Text = ((item.Parent.Health.Value).." / "..(currentHealth.Value))
			cloneHealth.TextLabel.Position = UDim2.new(0, cursor.Hit.p.X , 0, cursor.Hit.p.Y )
		
			
		end
	end
end)

i mean can u show the guis in the playergui not the script

I don’t get what you mean, that is the script that controls the position of the cloned gui, and this is the gui thing that is cloned
Screenshot 2021-12-28 at 10.35.23 PM
it is in replicated storage

And also I tried printing the position of the cursor, it does update
Screenshot 2021-12-28 at 10.36.45 PM