How to detect a moving part's position with a keybind

  1. What do you want to achieve?
    I’m trying to create a game like RoBeats, I have gotten far and I can move the notes by cloning them onto workspace tweening them. My goal now is to make the buttons work but I don’t know how.

  2. What is the issue?
    I’ve been struggling for a few days trying to detect a moving part’s position with the press of a key. Another problem is that the notes ignore collisions as they are being tweened.

  3. What solutions have you tried so far?
    I’ve tried methods like hitboxes and position tracking but none of them worked.

The Keybind script is a LocalScript located in StarterCharacterScripts, and this is currently what it has:

-- This is only for one lane.
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local Beams = ReplicatedStorage:WaitForChild("Beams")
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
debounce = false

UserInputService.InputBegan:Connect(function(Input, IsTyping)
    if IsTyping then return end
        if Input.KeyCode == Enum.KeyCode["A"] then
        -- This just creates the light effect when the key is pressed.
        local Beam = Beams.A:Clone()
        Beam.Parent = game:GetService("Workspace")

        local Properties = {
            Size = Vector3.new(17, 0.4, 0.4);
            Transparency = 1
        }

        ocal Info = TweenInfo.new(0.4)
        local Tween = TweenService:Create(Beam, Info, Properties)

        Tween:Play()
        wait(0.4)
        Beam:Destroy()
    end
end)

How to detect a moving part’s position with a keybind?

Thanks in advance for the help!

1 Like

Why not get the parts CFrame every time they click it?

1 Like

You can also use magnitude with both of the buttons position to check if they clicked said button on time.

2 Likes

I’ve been thinking you could use BasePart:GetTouchingParts().

2 Likes

I may suggest trying this:

local position
while wait() do
    for _, v in ipairs(workspace:GetChildren()) do
       v.Changed:Connect(function(property)
          if property == "Position" then
            warn("Position of".. tostring(v.Name).. " has changed.")
            
            position = v.Position
          end
       end
    end
end
2 Likes