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

How to fix attempt to concatenate string with userdata ?

Asked by 5 years ago
Edited 5 years ago

Hello! I was researching about player input and i tried a script

01local Players = game:GetService("Players")
02local player = Players.LocalPlayer
03local mouse = player:GetMouse()
04 
05local function onMouseClick()
06    print(player.Name .. " clicked at position: " .. mouse.Hit.p)
07    if mouse.Target then
08        print("Clicked part: " .. mouse.Target:GetFullName())
09    else
10        print("No part clicked")
11    end
12end
13mouse.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

1mouse.Hit.p.X..mouse.Hit.p.Y..mouse.Hit.p.Z

Is there a more efficient way to do this?

1 answer

Log in to vote
2
Answered by
ScuffedAI 435 Moderation Voter
5 years ago
Edited 5 years ago

Yes. By wrapping Vector3 in a tostring function, the game will return a string similar to your second code block.

1local SomeVector = Vector3.new()
2 
3print("My vector " .. tostring(SomeVector))
4-- Expected result: My vector 0, 0, 0

This also works for other different types of objects as well.

Ad

Answer this question