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

Attempt to index local 'plr' (a nil value)?

Asked by 5 years ago
Edited 5 years ago

I am trying to create a custom admin bar with working admin commands. Right now, I'm trying to make a damage command. The problem is in the server script, line 10.

I'll add the server script and local script (if needed), may anyone tell me what's wrong with my code?

--LOCAL SCRIPT--

wait()

local rf = game:FindFirstChild("ReplicatedStorage").RemoteEvents.apcmd
local ok = script.Parent.submitcmd
local bar = script.Parent.cmdbar

ok.MouseButton1Click:Connect(function()
    rf:FireServer(bar.Text)
end)
--SERVER SCRIPT--

local plrs = game:GetService("Players")
local rf = game.ReplicatedStorage.RemoteEvents.apcmd

rf.OnServerEvent:Connect(function(player,cmd)

    if cmd:sub(1,7) == "damage " then
        local plr = plrs:FindFirstChild(cmd:sub(8))
        game.Workspace[plr.Name].Humanoid.Health = game.Workspace[plr.Name].Humanoid.Health - cmd:sub(10)
end
end)
0
which line of script did you get the error? OnaKat 444 — 5y

1 answer

Log in to vote
0
Answered by
OnaKat 444 Moderation Voter
5 years ago
Edited 5 years ago

ok my second answer. I see the problem.

cmd = "damage "..PlayerName..Damage

In your script use sub(8) that will be PlayerName..Damage so that not the player name.

now try this script.

--SERVER SCRIPT--

local plrs = game:GetService("Players")
local rf = game.ReplicatedStorage.RemoteEvents.apcmd

rf.OnServerEvent:Connect(function(player,cmd)

    if cmd:sub(1,7) == "damage " then
        local plr = nil
        for _,v in pairs(plrs:GetPlayers()) 
            for i = 8,40,1 do
                if cmd:sub(8,i) == v.Name then
                    plr = v
                end
            end
        end
        if plr ~= nil then
            plr.Character.Humanoid.Health = plr.Character.Humanoid.Health - cmd:sub(10)
        else
            print("No Player Found")
        end
    end
end)
0
Sorry for a late response, but now I'm getting an error message (attempt to perform arithmetic on a string value)... InstantManager 27 — 5y
Ad

Answer this question