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

Attempt to index field?

Asked by 7 years ago
Edited 7 years ago

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)
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 M39a9am3R 3210 — 7y
0
Thank you, it'll try that out! SpazzMan502 133 — 7y

1 answer

Log in to vote
1
Answered by
Pixxels 45
7 years ago
Edited 7 years ago

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
0
Thank you! This is the first time I'm scripting a tool from scratch, so I knew I would run into problems. SpazzMan502 133 — 7y
Ad

Answer this question