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

Error with :GetMouse().Target when inserting it into a StringValue?

I'm making it so when you click on a thing named Tool, it gets in your inventory. But this prints attempt to concatenate field 'Target' (a userdata value) and the wiki had no answers. Here is what I tried.

local Mouse = game.Players.LocalPlayer:GetMouse()
local Debounce = false

Mouse.Button1Down:connect(function()
	if Mouse.Target ~= nil and Debounce == false then --checks if a part is clicked
		script.ClickPart.Value = Mouse.Target
		if script.ClickPart.Value == "Tool" then
			print("Yay")
			Debounce = true
			local Inventory = script.Parent.Parent.Inventory
			Instance.new("StringValue", Inventory.Things)
			Inventory.Things.Value.Value = Mouse.Target
			Inventory.Things.Value.Name = "Item"
			wait(.35) --so they don't spam click
			Debounce = false
		end
	end
end)

I thought you could do it, like how you can do this

print(game.Workspace.Baseplate.BrickColor.." is the colo(u)r of the baseplate!")

But this also prints another error! It says "attempt to concatenate field 'BrickColor' (a userdata value)". Why does it say it is a userdata value? But why can you do this?

print("The transparency of the baseplate is "..game.Workspace.Baseplate.Transparency)

I don't get it.

2 answers

Just simply convert the value to string.

if tostring(script.ClickPart.Value) == "Tool" then

2 comments

0

I dont get what tostring does.

0

plus the problem is on line 6

What is 'userdata'?

The userdata type in Lua is a data type that does not have a set of pre-defined operations like the string, function, or number. Many ROBLOX objects (like Parts, Models, StringValues) or ROBLOX values (BrickColor, Vector3) fall into this category.

The Problem

script.ClickPart.Value = Mouse.Target
-- Corrected.
script.ClickPart.Value = Mouse.Target.Name

On line 6 and line 12, you are trying to set a StringValue's Value property to the Target property of the mouse. However, the Target can only either be a ROBLOX part or nil. To correct the script, you have to access the name of the object you're clicking on, not the object itself.

Additional Comments

You can concatenate Transparency because the Transparency property is a number. You cannot concatenate brick color because, even though you can set BrickColor using a string, the BrickColor property is not a string.

Keep this in mind - if there is a property strongly associated with an object (like its name), it is easy to think that the object will act like that property. Your script will not make this assumption.

1 comment

0

Incidentally, statements like print(game.Workspace.StringValue) work with game objects because Studio does quite a bit of coercion when it doesn't get a type it expects.

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