Hello! I was researching about player input and i tried a script
local Players = game:GetService("Players") local player = Players.LocalPlayer local mouse = player:GetMouse() local function onMouseClick() print(player.Name .. " clicked at position: " .. mouse.Hit.p) if mouse.Target then print("Clicked part: " .. mouse.Target:GetFullName()) else print("No part clicked") end end mouse.Button1Down:Connect(onMouseClick)
This script is a local script and it was put in StarterPlayerScripts
When i was testing, the output said
Players.Nguyenlegiahung.PlayerScripts.LocalScript:6: attempt to concatenate string with userdata
I changed line 6 to
print(mouse.Hit.p)
and it was working, but how do I concatenate this?
Hope you can help :D
Thanks for reading my bad English
Edit: I was able to fix it by doing
mouse.Hit.p.X..mouse.Hit.p.Y..mouse.Hit.p.Z
Is there a more efficient way to do this?
Yes. By wrapping Vector3
in a tostring
function, the game will return a string similar to your second code block.
local SomeVector = Vector3.new() print("My vector " .. tostring(SomeVector)) -- Expected result: My vector 0, 0, 0
This also works for other different types of objects as well.