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

How to find the closest humanoid with a local script?

Hi, I am trying to figure out how to make a script that blasts the person closest to you. So far I have not gotten any luck trying to figure out how to attack the nearest person.

This is what I have got:

local player = game.Players.LocalPlayer
character = player.CharacterAdded:wait()
local humanoid = character.HumanoidRootPart
local health = character.Humanoid

local mouse = player:GetMouse()
mouse.KeyDown:connect(function(key)
    if key == "f" then  --Ignore this power--
        game:GetService("Chat"):Chat(player.Character.Head, "SPIRIT WALK!","Green")
        humanoid.Size = Vector3.new(2,1,1)
        humanoid.Transparency = 0.5
        humanoid.CanCollide = false
        player.PlayerGui.Timer.TextLabel.Visible = true
	    wait(1)
	    player.PlayerGui.Timer.TextLabel.Text = ("4")
	    wait(1)
	    player.PlayerGui.Timer.TextLabel.Text = ("3") 
	    wait(1)
	    player.PlayerGui.Timer.TextLabel.Text = ("2") 
	    wait(1)
	    player.PlayerGui.Timer.TextLabel.Text = ("1")
	    wait(1)
	    player.PlayerGui.Timer.TextLabel.Visible = false   
	    health.Health = 0
    elseif key == "e" then   --this is the power I am stuck on!--
	    
    end
end)


Thank you for the help

1 answer

Why is your text wrapped in brackets line in lines 15-21? Why are you using KeyDown? KeyDown is deprecated. Also, please make your variables correspond to their value, like why did you name your Humanoid variable health and your humanoidrootpart humanoid? Let me fix this mess

local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait() -- :wait() is deprecated
local timer = plr:WaitForChild("PlayerGui").Timer.TextLabel

function onKeyPress()
    game:GetService("Chat"):Chat(
        char.Head,
        "SPIRIT WALK!",
        Enum.ChatColor.Green -- Enum.ChatColor not "Color"
)

    char.HumanoidRootPart.Size = Vector3.new(2, 1, 1)
    char.HumanoidRootPart.Transparency = .5
    char.HumanoidRootPart.CanCollide = false
    timer.Visible = true
    for i = 4, 0, -1 do -- instead of manually setting the text to a number do this
        timer.Text = tostring(i)
        if i == 0 then
            timer.Visible = false 
            break
            
        end
        char.Humanoid:TakeDamage(char.Humanoid.MaxHealth)
        wait(1)
    end
end

game:GetService("ContextActionService"):BindAction(
    "Power",
    onKeyPress,
    false,
    Enum.KeyCode.F
)

3 comments

1

Those are actually Parenthesis, not brackets. These are brackets [], these are Parenthesis (), and these are Braces {}.

0

where i live they’re called brackets

0

oh, ok but umm I have a question. Whats the difference between Parenthesis and Brackets, in scripting?

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