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

Attempt to index field?

Hi, I'm trying to make a tool that makes a part appear and the part moves to the player's mouse's Position.

I'm trying to use the Changed event instead of a while true do wait loop, but it doesn't seem to be working.

I'm getting this error:

14:46:38.400 - Players.Player1.Backpack.Build.LocalScript:13: attempt to index field 'x' (a number value)

I don't see why it's trying to index it when it's a number value. Also, is there any event that fires when Any part of the CFrame is changed? So I don't need to rely on just .x or something.

This is the script.

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local CF = mouse.Hit

part = Instance.new("Part")
part.Parent = game.ReplicatedStorage
part.Anchored = true
part.BrickColor = BrickColor.Red()

script.Parent.Equipped:connect(function(mouse)
	print("Equipped")
	part.Parent = workspace
CF.x.Changed:connect(function()
	part.Position = Vector3.new(CF.p)
end)
end)

2 comments

1

Because .Changed can not be used on number values. Indexing means finding something within something. In your case you're trying to index into x which is a number value. Use mouse.Move http://wiki.roblox.com/index.php?title=API:Class/Mouse/Move

0

Thank you, it'll try that out!

1 answer

✓ Accepted answer

When writing..

local CF = mouse.Hit

..you're setting the CF value, once. The CF variable will be static since you never assign a new value to it (CF never changes).

If you look here: http://wiki.roblox.com/index.php?title=API:Class/PlayerMouse you'll find that there's an event called "move". I'd use that event to detect when the mouse is moved and then move the part accordingly.

So:

-- Equipping the tool
script.Parent.Equipped:connect(function()
part.Parent = workspace
	-- Detecting when the mouse is moved
	mouse.Move:connect(function()
		-- Setting the part's position
		part.CFrame = mouse.Hit.p
	end)
end)

Also, I'd consider setting the mouse's targetfilter to avoid certain complications. Try without first and you'll see what happens. You'll notice the part starts to float towards the screen. Setting the mouse's targetfilter will make the mouse ignore the part and all it's descendants so the position that the mouse.Hit returns is the position behind the part you're moving.

mouse.TargetFilter = part

1 comment

0

Thank you! This is the first time I'm scripting a tool from scratch, so I knew I would run into problems.

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