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:

script.Parent.Equipped:Connect(function(mouse)
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true
    mouse.Button1Down:Connect(function()
        print(mouse.Target)
        game.Players.LocalPlayer:WaitForChild("PlayerGui"):FindFirstChild("ScreenGui"):FindFirstChild("Frame"):FindFirstChild("TextLabel").Text = mouse.Target
    end)
end)

script.Parent.Unequipped:Connect(function()
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = false
end)

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

if mouse.Target ~= nil then -- ~= means not equal
    print("")
end
-- this is a seperate script for detecting if there is an object or not
script.Parent.Equipped:Connect(function(mouse)
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = true
    mouse.Button1Down:Connect(function()
        print(mouse.Target) -- print always converts to string
        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
    end)
end)

script.Parent.Unequipped:Connect(function()
    game.Players.LocalPlayer.PlayerGui.ScreenGui.Frame.Visible = false
end)
Ad

Answer this question