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

How to print Mouse.Hit.p without error of hit.p being a userdata value?

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
plrs = {}
for i,v in pairs(game.Players:GetChildren()) do
table.insert(plrs,v:GetFullName()
end
for i = 1,#plrs do
local mouse = plrs[i]:GetMouse()

local function onInput(inputObject, gameProcessed)
    if gameProcessed then return end

    if inputObject.UserInputType == Enum.UserInputType.Touch then
    target = mouse.Hit.p
 print(mouse.Hit.p)
    end
end

UIS.InputBegan:Connect(onInput)

Okay - I understand something else might error because I compiled this script at like 2 AM. When I try to print "mouse.Hit.p", it gives me an error stating that you cannot print a userdata value. Is there any fix to this? Help is greatly appreciated.

2 answers

✓ Accepted answer

To convert userdata to a string, we can use a built-in roblox LUA function called tostring(). In your case, it would be used as so:

local UIS = game:GetService("UserInputService")
local Players = game:GetService("Players")
plrs = {}
for i,v in pairs(game.Players:GetChildren()) do
table.insert(plrs,v:GetFullName()
end
for i = 1,#plrs do
local mouse = plrs[i]:GetMouse()

local function onInput(inputObject, gameProcessed)
    if gameProcessed then return end

    if inputObject.UserInputType == Enum.UserInputType.Touch then
    target = mouse.Hit.p
 print(tostring(mouse.Hit.p))
    end
end

UIS.InputBegan:Connect(onInput)

As seen on line 15, we added the tostring function inside of print() with the argument as what we want to convert to a string. It should now either print the proper vectors or nil if your pointing at the sky. With further questions regarding this topic, feel free to reply to my post.

2 comments

0

Thank you so much! This was giving me a lot of trouble :P

0

My pleasure!

Line 5 needs to change to

table.insert(plrs,v)

:GetFullName() returns a string and not the actual instance, thus failing to capture the mouse on line 8. GetMouse() can only be called from a player and not a string.

3 comments

0

Ok, thanks! I can upvote but since it's ot what im looking for i sadly can't accept your answer :c

0

No problem. Glad to help.

0

Use comments for misc corrections like that

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