Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

[SOLVED] How do make it when I hoverover a Part that has BoolValue UI appears and follows the mouse?

Information:

  • I am not getting any errors
  • I already attempted this, it didn't work.
  • The parent of the script StarterCharacter.StarterPlayerScripts
  • The script is a LocalScript
  • The part is in Workspace.
  • The BoolValue is in the part.
  • There are multiple parts.

Attempt:

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()  

game.Players.PlayerAdded:Connect(function(player)
	UIS.InputChanged:Connect(function(input)
	if mouse.Target then
		if mouse.Target:FindFirstChild("BoolValue") then
			player.PlayerGui.CollectGUI.Enabled = true --ScreenGUI is enabled
			player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, mouse.X, 0, mouse.Y) --Frame follows mouse
			player.PlayerGui.CollectGUI.Frame.ItemName.Text = mouse.Target.Parent.Name -- ItemName.Text is the part's name
		else
			player.PlayerGui.CollectGUI.Enabled = false --ScreenGUI is disabled
			player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, 0, 0, 0) --Frame position is reset
			player.PlayerGui.CollectGUI.Frame.ItemName.Text = "" --ItemName.Text is reset
		end
	end
end)
end)

2 answers

✓ Accepted answer

Why did you put UserInputService in this? We are needing the user's mouse, not keyboard clicks. There is also no need for the player added event since we know that local scripts run for the current player only. At least I hope you used a LocalScript which you must

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()  


if mouse.Target then
    if mouse.Target:FindFirstChild("BoolValue") then
        player.PlayerGui.CollectGUI.Enabled = true --ScreenGUI is enabled
        player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, mouse.X, 0, mouse.Y) --Frame follows mouse
        player.PlayerGui.CollectGUI.Frame.ItemName.Text = mouse.Target.Parent.Name -- ItemName.Text is the part's name
    else
        player.PlayerGui.CollectGUI.Enabled = false --ScreenGUI is disabled
        player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, 0, 0, 0) --Frame position is reset
        player.PlayerGui.CollectGUI.Frame.ItemName.Text = "" --ItemName.Text is reset
    end
end

So you may wonder why that script is not working either.

Well you need a loop which constantly checks the mouse's Target.

local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()  

RunService.Stepped:Connect(function(step)
    --wait(step) --Optional if you want it to coordinate with user's frames
    if mouse.Target then
        if mouse.Target:FindFirstChild("BoolValue") then
            player.PlayerGui.CollectGUI.Enabled = true --ScreenGUI is enabled
            player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, mouse.X, 0, mouse.Y) --Frame follows mouse
            player.PlayerGui.CollectGUI.Frame.ItemName.Text = mouse.Target.Parent.Name -- ItemName.Text is the part's name
        else
            player.PlayerGui.CollectGUI.Enabled = false --ScreenGUI is disabled
            player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, 0, 0, 0) --Frame position is reset
            player.PlayerGui.CollectGUI.Frame.ItemName.Text = "" --ItemName.Text is reset
        end
    end
end)

I wish you goodluck on solving your issue if my answer did not help.

2 comments

1

Hello! I am here to inform you that in this case, RunService.Stepped is more useful then RunService.RenderStepped

0

Thanks for the info, I don't really know the difference. I appreciate you telling me that.

just remove the event called PlayerAdded. Local scripts can't seem to use that event. the following code is the fix.

local Players = game:GetService("Players")

local UIS = game:GetService("UserInputService")
 
	 UIS.InputChanged:Connect(function(input)

		local player = Players.LocalPlayer
		local mouse = player:GetMouse()  
    if mouse.Target then
        if mouse.Target:FindFirstChild("BoolValue") then
            player.PlayerGui.CollectGUI.Enabled = true --ScreenGUI is enabled
            player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, mouse.X+3, 0, mouse.Y+3) --Frame follows mouse
            player.PlayerGui.CollectGUI.Frame.ItemName.Text = mouse.Target.Parent.Name -- ItemName.Text is the part's name
        else
            player.PlayerGui.CollectGUI.Enabled = false --ScreenGUI is disabled
            player.PlayerGui.CollectGUI.Frame.Position = UDim2.new(0, 0, 0, 0) --Frame position is reset
            player.PlayerGui.CollectGUI.Frame.ItemName.Text = "" --ItemName.Text is reset
        end
    end
end)

Since we're inside a player via the local script its unnecessary to call that event for all players on the server. Now if this was a server side script then it would work!.

just incase your wondering on line 1 the:

local Players = game:GetService("Players")

is the same as:

local player = game.Players.LocalPlayer

2 comments

0

I don't know if this works, but I checked the other answer first that's why you didn't get your answer accepted if it was correct.

0

this answer only updates when you touch the mouse/keyboard, touch device. the other answer is running all the time. Both are valid though and fair dos, uhi_o answered just before mine i think a second before lol. as long as you got it working :).

This discussion is preserved as it was. New answers, comments, and votes are closed.