Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to use mouse.Target as string?

Asked by 4 years ago

Hello, devs!

I was doing tool that changes gui text when you will click at any object, but while trying to change the gui text with the mouse.Target, it said that you can't use mouse.Target, you need to use string. Can someone please explain me how to make that work?

CODE:

01script.Parent.Equipped:Connect(function(mouse)
02    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true
03    mouse.Button1Down:Connect(function()
04        print(mouse.Target)
05        game.Players.LocalPlayer:WaitForChild("PlayerGui"):FindFirstChild("ScreenGui"):FindFirstChild("Frame"):FindFirstChild("TextLabel").Text = mouse.Target
06    end)
07end)
08 
09script.Parent.Unequipped:Connect(function()
10    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = false
11end)

ERRORS:

Players.ErktikyYT.Backpack.Tool.LocalScript:2: invalid argument #3 (string expected, got Instance) Line 5

So at the end of the line 5. This is my problem.

0
Did you try using mouse.Target.Name? PoWerofThEePg 43 — 4y
0
It worked, but when I press on sky (nil) then it shows error on output. ErktikyYT 89 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

im not sure what youre trying to do but, mouse.target is the object the cursor is on. mouse.Hit is a cframe that has the mouse's position. also, from your comment you were getting an error because there was no object. this is how i personally would change it

1if mouse.Target ~= nil then -- ~= means not equal
2    print("")
3end
4-- this is a seperate script for detecting if there is an object or not
01script.Parent.Equipped:Connect(function(mouse)
02    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true
03    mouse.Button1Down:Connect(function()
04        print(mouse.Target) -- print always converts to string
05        game.Players.LocalPlayer:WaitForChild("PlayerGui"):FindFirstChild("ScreenGui"):FindFirstChild("Frame"):FindFirstChild("TextLabel").Text = tostring(mouse.Target) -- tostring makes it a string but in this case would be equivalent to it's name
06    end)
07end)
08 
09script.Parent.Unequipped:Connect(function()
10    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = false
11end)
Ad

Answer this question