I wanted to know if there is a way to get a player from mouse

I’m trying to make a tool that uses :GetMouse() from player, it doesn’t need to be with mouse it can be with another solution i just wanted to know if theres a way to do what i want to do, so basically what i want to do is a tool that tps behind the player

My script Doesn’t work does not matter if i use mouse.Target since it doesn’t Work

I’ve searched the whole night up on everywhere not found a single thing about it.

My current broken code ( local script on tool )

image

local player = game.Players.LocalPlayer
local chara = player.Character
local tool = script.Parent
local players = game.Players

local re = script.RemoteEvent
local canab = game.ReplicatedStorage.Values.CanAbility

local mouse = player:GetMouse()
if tool.Equipped then
	if mouse.Hit.p == player.Position and tool.Triggered then
	end
end

local animationId = "99910051876265"

local animationPriority = Enum.AnimationPriority.Action

local function playAnimation(player)
	local character = player.Character
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		local animation = Instance.new("Animation")
		animation.AnimationId = "rbxassetid://" .. animationId

		local animationTrack = humanoid:LoadAnimation(animation)
		animationTrack.Priority = animationPriority
		animationTrack:Play()
	end
end

if canab.Value == true then
	tool.Activated:Connect(function()
		script.Parent.Handle.gr:Play()
		local Player = game.Players:GetPlayerFromCharacter(tool.Parent)
		if Player then
			playAnimation(Player)
		end
		re:FireServer(tool)
	end)
end

Yes, you can. Mouse.Target yields the BasePart the client’s cursor is hovering over in 3D space. Players in 3D space are represented through avatars, which are comprised of BaseParts. The majority of the BaseParts in an avatar are direct descendants (children) of that avatar Model in Workspace. These direct descendants are the avatar’s limbs. The remaining BaseParts are the handles of accessories, tools, and the like.

With the knowledge that player’s are associated with their avatars, and the potential for the BasePart referred to by Mouse.Target to be a descendant of that avatar, you can build a function to search for the descendant’s overarching Model and cross-reference that model with a player:

local function getCharacterFromDescendant(descendant: Instance): Model?
	local parent = descendant

	while true do
		parent = parent:FindFirstAncestorOfClass("Model") :: Model

		if not parent then
			return
		elseif parent.Parent == workspace then
			break
		end
	end

	if not Players:GetPlayerFromCharacter(parent) then
		return
	end

	return parent :: Model
end

does that mean i can just detect it from the humanoid root part?

Sure, if you would like to handle it that way

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.