Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How to find the closest humanoid with a local script?

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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
)
1
Those are actually Parenthesis, not brackets. These are brackets [], these are Parenthesis (), and these are Braces {}. FlynnXRider 4 — 5y
0
where i live they’re called brackets User#19524 175 — 5y
0
oh, ok but umm I have a question. Whats the difference between Parenthesis and Brackets, in scripting? turbomegapower12345 48 — 5y
Ad

Answer this question