script.Parent.Activated:Connect(function()
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
Player.Character.PrimaryPart.CFrame = CFrame.new(mouse.Hit.p)
end)
Nope! You were trying to get the player from the function, which works in some cases, but getting the player’s mouse requires you to check the player object, not a player reference.
The .Activated event when fired does not automatically pass the player object pertaining to the client which fired the event as an argument to any callback function connected to it, all you needed to do and what you should do is the following.
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
local mouse = player:GetMouse()
local tool = script.Parent
tool.Activated:Connect(function()
hrp.CFrame = mouse.Hit
end)
This will also avoid redeclaring the same variables each time “Activated” fires, they only need to be declared once, as they are constants (variables with values which are not subject to change), hence their declarations can be moved outside of the callback function connected to the event.
Don’t forget that “mouse.Hit” is a CFrame value already so you can also just use that instead of constructing a new CFrame value by using the Vector3 value held by “mouse.Hit.p”.