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

How do I get a humanoid from mouse.target?

The goal of my script/tool it to arrest and release people. The people arrested will be sent to a certain team and the same goes for the release function. Currently the arrest function works while the release dose not even though the code is almost identical. Any help is appreciated

--By: VineyardVine


local otherperson = nil
local otherhumanoid
mode = 1
sp = script.Parent
local me = game.Players.LocalPlayer
local Mouse = game.Players.LocalPlayer:GetMouse()
local teamcivi = game.Teams:FindFirstChild("Civilian")
local teamcolor = teamcivi.TeamColor
local on = 0

function equipped()
	on = 1
end

function disequipped()
	on = 0
end

function changemode(key, isDown)
	key = key:lower()
	if on == 1 then
	if key == "q" and mode == 1 then
		mode = 2
		script.Parent.Name = "Release Mode"
	    elseif key == "e" and mode == 2 then
		mode = 1 
		script.Parent.Name = "Arrest Mode"
	end
	end
end

function arrest()
	    print("Pass1")
    if mode == 1 then	
	local target = Mouse.Target
	if target and target.Parent then
	local humanoid = target.Parent:FindFirstChild("Humanoid")
		if target.Parent:IsA("Hat") and target.Parent.Parent then
			local humanoid = target.Parent.Parent:FindFirstChild("Humanoid")
		end
			print("Pass2")
	if humanoid then
		otherperson = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		otherhumanoid = humanoid
	end
		print("Pass3")
	if otherperson and otherhumanoid and me:DistanceFromCharacter(humanoid.Parent.Head.Position) <= 10 and otherperson.TeamColor ~= BrickColor.new("Really black") then
			otherperson.TeamColor = BrickColor.new("Really black")
			otherhumanoid.Parent:BreakJoints()
		end
	end
	    print("Pass4")
end
end

function release()
	if mode == 2 then
	print("Pass1")
	local target = Mouse.Target
	if target and target.Parent then
	humanoid = target.Parent:FindFirstChild("Humanoid")
		if target.Parent:IsA("Hat") and target.Parent.Parent then
		   humanoid = target.Parent.Parent:FindFirstChild("Humanoid")
		end
		print("Pass2")
	if humanoid then
		otherperson = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		otherhumanoid = humanoid
	end
	print("Pass3")
	if otherperson and otherhumanoid and me:DistanceFromCharacter(humanoid.Parent.Head.Position) <= 5 and mode == 2 then
			otherperson.TeamColor = teamcolor
			otherhumanoid.Parent:BreakJoints()
	end
	end
	end
end

sp.Equipped:connect(equipped)
sp.Unequipped:connect(disequipped)
Mouse.KeyDown:connect(function(key) changemode(key, true) end)
Mouse.KeyUp:connect(function(key) changemode(key, false) end)
sp.Activated:connect(arrest,release)


1 answer

To get the Humanoid from Mouse.Target, you could use a > check as such:

function getModel(raw)
for _,__ in next game.Workspace:children'' do
if raw:IsDescendantOf(__) then
return __;
end;
end;
return raw;
end;

local success,hum = pcall(function() return getModel(mouse.Target).Humanoid end);
hum.Walkspeed = 10;

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