Change mouse if hovering over the object

i got this script from @starmaq and it makes the player able to click and drag objects, i was wondering if it was possible to make it so that if you hover over the certain part (an unanchored part you can drag) the mouse would become a small ball, to indicate its possible to be dragged around

local player = game.Players.LocalPlayer 
local mouse = player:GetMouse() 
local camera = workspace.CurrentCamera
local RunService = game:GetService("RunService") --we will use this service for something later
local target 
local down



mouse.Button1Down:connect(function() 
	if mouse.Target ~= nil and mouse.Target.Locked == false then
		target = mouse.Target 
		mouse.TargetFilter = target 
		down = true 
		
		local gyro = Instance.new("BodyGyro") --adding the forces
		gyro.Name = "Gyro"
		gyro.Parent = target 
		gyro.MaxTorque = Vector3.new(500000, 500000, 500000)
		local force = Instance.new("BodyPosition") 
		force.Name = "Force" 
		force.Parent = target
		force.MaxForce = Vector3.new(10000, 10000, 10000) --you may wanna modify this a bit, since it effect if you can move an object or wrong depending on its weight/mass (in other words, the force might not be strong enough)
	end
end)

game:GetService("RunService").RenderStepped:Connect(function() --replaced the move event with renderstepped because it works better in some cases, renderstepped is an event that fires every frame, basically super fast, look it up it is important!
	if down == true and target ~= nil then 
		target.Gyro.CFrame = target.CFrame --this is to keep rotation
		target.Force.Position = camera.CFrame.Position + (mouse.UnitRay.Direction * 20)
	end 
end) 

mouse.Button1Up:connect(function() 
	if target then --of course we wanna remove the forces after the dragging, first check if there was even a target
		if target:FindFirstChild("Gyro") or target:FindFirstChild("Force") then --check if there was a force
			target.Gyro:Destroy() --DESTROY!!!!
			target.Force:Destroy()
		end
	end
	down = false
	mouse.TargetFilter = nil
	target = nil 
end)
1 Like

Pretty sure there are multiple ways to do this.
You can very often check if the Mouse target (Mouse.Target) is the object, and if it is, change the icon (Mouse.Icon).
Or you can add a click detector to the object and use: ClickDetector.MouseHoverEnter and ClickDetector.MouseHoverLeave to call the functions that change mouse.Icon
Resources:
Click Detector for MouseHoverEnter/Leave
Mouse (for the icon and target)

1 Like

can you help script it though? i dont know how to do it myself

1 Like

Yes, I can give examples:
Example 1 with click detectors:
Put a click detector in the part.

--This is a local script in the StarterPlayerScripts
local mouse = game.Players.LocalPlayer:GetMouse()
local DraggablePart = nil --Replace nil with your part
local iconId = nil --Replace nil with the id
DraggablePart.ClickDetector.MouseHoverEnter:Connect(function() -- On hover enter.
	mouse.Icon = iconId
end)
DraggablePart.ClickDetector.MouseHoverLeave:Connect(function() --On hover exit do:
	mouse.Icon = [[]] --:Set the Icon to default
end)

Example 2 using Mouse.Target:

--This is a local script in the StarterPlayerScripts
local mouse = game.Players.LocalPlayer:GetMouse()
local RunService = game:GetService("RunService")
local DraggablePart = nil --Replace nil with your part
local iconId = nil --Replace nil with the Image ID

RunService.RenderStepped:Connect(function() --Every frame:
	if(mouse.Target == DraggablePart) then --If the target is the part
		mouse.Icon = icon --Set it to your Icon
	else
		mouse.Icon = [[]]--Set it to default
	end
end)
1 Like

where do i insert the decal id for the mouse? and where does this script go? (local or normal?)

It’s not 1 script, I gave you 2 examples of how to do it. And I said where you should put the Decal Id and the part in the script.

You can choose which one you prefer.

1 Like

Okay then my last question is, instead of putting the name of the part inside the script what about its “if the part is named Draggable it changes the mouse otherwise it doesn’t” and also if it’s a group with all the parts welded together how will that work out?

Do you mean that it’ll happen to every part called draggable?

1 Like

Yes, and my question is will it also work on groups named Draggable

It’ll work for parts. If you create a folder called DraggableParts and put everything in there without renaming them it’ll be easier.
I’ll change the example and put it here:

1 Like

Sadly, the unanchored parts are already in a folder so that all parts in that folder can regenerate. Since the game can move parts and accidentally fall of the map they should regenerate every x min. So i don’t know how that will work out otherwise (and also imagine the folder thing works out together with regen, even if there are groups in there will it work?)

Do you mean that the parts are already in a folder, and some of them are being added/removed?

1 Like

Yes, there is a folder called regen parts and all parts in there get regenerated every idk min, if I add new anchored parts I still add more to the folder yes

1 Like

Then I suggest you to use my second example, here I changed it a bit:
Example 2 using Mouse.Target:

--This is a local script in the StarterPlayerScripts
local mouse = game.Players.LocalPlayer:GetMouse()
local RunService = game:GetService("RunService")
local DraggableParts = nil --Replace nil with your folder
local iconId = nil --Replace nil with the Image ID

RunService.RenderStepped:Connect(function() --Every frame:
	if(mouse.Target ~= nil) then --If the target isn't nothing
		if(mouse.Target:IsDescendantOf(DraggableParts)) then --If the target is in the folder
			mouse.Icon = icon --Set it to your Icon
		else
			mouse.Icon = [[]]--Set it to default
		end
	else
		mouse.Icon = [[]]--Set it to default
	end
end)

But replace DraggableParts’s nil with the folder’s path, e.g. workspace.RegenParts
Every frame it’ll check if the mouse Target is something in the Folder, so adding and removing parts from there is not a problem.

1 Like

Will test when I’m back, thanks!

1 Like

No problem :slightly_smiling_face:, I made a small change to the script, so if you didn’t try it yet it’s fine.

1 Like

im at my laptop but im confused. at one point it says to change something to my icon, but idk what to put there?

The IconId variable? It needs to be “rbxassetid://Your Decal Id”
Replace Your Decal Id with the id of the image

You can check if part is member of that folder or you can also use Collection Service, so you don’t need to parent each and every part. Its your wish after all though.

–This is a local script in the StarterPlayerScripts
local mouse = game.Players.LocalPlayer:GetMouse()
local RunService = game:GetService(“RunService”)
local DraggableParts = workspace[“Regen Parts”]
local iconId = 108939221

RunService.RenderStepped:Connect(function() --Every frame:
if(mouse.Target ~= nil) then --If the target isn’t nothing
if(mouse.Target:IsDescendantOf(DraggableParts)) then --If the target is in the folder
mouse.Icon = icon --Set it to your Icon
else
mouse.Icon = [[]]–Set it to default
end
else
mouse.Icon = [[]]–Set it to default
end
end)

at one point it says mouse icon = icon set it to your icon, what goes there