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

How to use mouse.Target as string?

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.

2 comments

0

Did you try using mouse.Target.Name?

0

It worked, but when I press on sky (nil) then it shows error on output.

1 answer

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)

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