|
| 21 Sep 2011 02:01 PM |
I want a text box in the player's gui to change based on what the player's mouse is hovering over. How do I do this? (General outline, not all the exact code)
Currently I'm trying - StringValue in brick named MouseTarget - script under textbox in the gui that updates the textbox's text when the player's 'MouseTarget' stat (located as a string value at Player.stat.MouseTarget) changes -Here's where I get stuck: It worked when I used mouse.Move() and mouse.Idle() under a tool's script, but that's a shaky way to do it, so how can I use mouse.Changed() with Target as a property to update the player's stat?
Here's all the code
------CHILD OF TOOL function findTarget(part) local e = part:findFirstChild("MouseTarget") if e ~= nil then return part.MouseTarget else return nil end end
function onMouseHover(mouse)
local target = mouse.Target
while true do wait(1) if target ~= nil then
local p = nil p = findTarget(target)
if p ~= nil then
script.Parent.Parent.Parent.stats.MouseTarget.Value = target.MouseTarget.Value elseif p == nil then script.Parent.Parent.Parent.stats.MouseTarget.Value = "-None-" end end end end
function onSelected(mouse) mouse.Button1Down:connect(function() onButton1Down(mouse) end) mouse.Move():connect(function() onMouseHover(mouse) end) mouse.Idle():connect(function() onMouseHover(mouse) end)
end
tool.Selected:connect(onSelected)
--CHILD OF SCREENGUI.etc.etc.TEXTLABEL
local label = script.Parent
wait(1)
local stat = script.Parent.Parent.Parent.Parent.Parent.Parent.stats.MouseTarget
stat.Changed:connect(function() local newVal = stat.Value label.Text = newVal end)
And of course Player.stats.MouseTarget is created upon entry and the target brick has a stringvalue with name "MouseTarget" |
|
|
| Report Abuse |
|
|
myrco919
|
  |
| Joined: 12 Jun 2009 |
| Total Posts: 13241 |
|
| |
|
| |
|
|
| 21 Sep 2011 02:37 PM |
| you could try the Idle and Move event of the mouse. the Idle fires when your mouse isn't moving and the Move event fires when your mouse moves. i just learned about them i few minutes ago :P |
|
|
| Report Abuse |
|
|
Miro034
|
  |
| Joined: 07 Oct 2009 |
| Total Posts: 6568 |
|
|
| 21 Sep 2011 02:52 PM |
Here :P xD
function onMouseHover(mouse) local target = mouse.Target
while true do wait(1) if mouse.Target ~= nil then
local p = nil p = findTarget(target)
if p ~= nil then
script.Parent.Parent.Parent.stats.MouseTarget.Value = mouse.Target.MouseTarget.Value elseif p == nil then script.Parent.Parent.Parent.stats.MouseTarget.Value = "-None-" end end end end
function onSelected(mouse) mouse.Button1Down:connect(function() onButton1Down(mouse) end) mouse.Move:connect(function() onMouseHover(mouse) end) mouse.Idle:connect(function() onMouseHover(mouse) end)
end |
|
|
| Report Abuse |
|
|
|
| 21 Sep 2011 02:59 PM |
Miro, MrGames: Are you two serious? You just gave me back the code I already have
function onSelected(mouse) mouse.Button1Down:connect(function() onButton1Down(mouse) end) mouse.Move():connect(function() onMouseHover(mouse) end) mouse.Idle():connect(function() onMouseHover(mouse) end)
the ()'s are typos... |
|
|
| Report Abuse |
|
|
|
| 21 Sep 2011 02:59 PM |
| lol, i just failed there. i didn't see that XD |
|
|
| Report Abuse |
|
|
Miro034
|
  |
| Joined: 07 Oct 2009 |
| Total Posts: 6568 |
|
| |
|
|
| 21 Sep 2011 03:43 PM |
So does anybody know how to do this? The problem is that the current function to detect mouse moving and idling causes a 1 second delay before changing the text. I would like it to be instant but removing the wait makes the while loop crash the program. |
|
|
| Report Abuse |
|
|