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

attempt to index nil with 'Name' - Giving Tool?

Asked by
kjduck 0
4 years ago
Edited 4 years ago

Hello all,

I am trying to give a player a tool by clicking on him/her.

local gui = script:WaitForChild("ToolGiver")
local toolButton = script:WaitForChild("Tool")
local groupId = 6301077
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local givingEvent = game.ReplicatedStorage:WaitForChild("GivePlayerTools")

if plr:GetRankInGroup(groupId) > 240 then 
    wait(1)
    script:Destroy()
end

mouse.Button1Down:Connect(function()
    local target = mouse.Target
    if mouse.target ~= nil and target.Parent:FindFirstChild("HumanoidRootPart") then
        local givingPlayer = game.Players:GetPlayerFromCharacter(target.Parent)
        local tools = plr.Backpack:GetChildren()
        local currentGui = gui:Clone()
        local givingTools = {}
        local giveButton = currentGui:WaitForChild("MainFrame"):WaitForChild("Give")
        local replicatedFolder = Instance.new("Folder", game.ReplicatedStorage)

        if plr.PlayerGui:FindFirstChild("ToolGiver") then
            plr.PlayerGui:FindFirstChild("ToolGiver"):Destroy()
        end
        currentGui.MainFrame.Player.Text = givingPlayer.Name
        currentGui.Parent = plr.PlayerGui
        for _, tool in pairs (tools) do
            local currentButton = toolButton:Clone()
            currentButton.Text = tool.Name
            currentButton.Parent = currentGui.MainFrame.Tools
            currentButton.MouseButton1Click:Connect(function()
                if currentButton.BackgroundTransparency == 0.3 then
                    table.remove(givingTools, tool)
                    currentButton.BackgroundTransparency = 0.8
                else
                    table.insert(givingTools, tool)
                    currentButton.BackgroundTransparency = 0.3
                end
            end)
        end
        giveButton.MouseButton1Click:Connect(function()
            currentGui:Destroy()
            for _, tool in pairs (givingTools) do
                tool.Parent = replicatedFolder
            end
            givingEvent:FireServer(replicatedFolder, givingPlr)
        end)
    end
end)

LINE 26 IS RETURNING WITH "Players.kjduck.PlayerScripts.ToolGivingLocal:26: attempt to index nil with 'Name' "

Any help would be appreciated :D

0
Can you put the script on the website i rather not click or download files IcyMizu 122 — 4y
0
Is this a Script or a LocalScript? cucucu0001 35 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

You didn't check if the mouse.Hit was a player or not. Updated Code:

local gui = script:WaitForChild("ToolGiver")
local toolButton = script:WaitForChild("Tool")
local groupId = 6301077
local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()
local givingEvent = game.ReplicatedStorage:WaitForChild("GivePlayerTools")

if plr:GetRankInGroup(groupId) > 240 then
  wait(1)
  script:Destroy()
end

mouse.Button1Down:Connect(function()
local target = mouse.Target
if mouse.target ~= nil and target.Parent:FindFirstChild("HumanoidRootPart") then
  local givingPlayer = game.Players:GetPlayerFromCharacter(target.Parent)
  local tools = plr.Backpack:GetChildren()
  local currentGui = gui:Clone()
  local givingTools = {}
  local giveButton = currentGui:WaitForChild("MainFrame"):WaitForChild("Give")
  local replicatedFolder = Instance.new("Folder", game.ReplicatedStorage)
  if (givingPlayer) then
    if plr.PlayerGui:FindFirstChild("ToolGiver") then
      plr.PlayerGui:FindFirstChild("ToolGiver"):Destroy()
    end
    currentGui.MainFrame.Player.Text = givingPlayer.Name
    currentGui.Parent = plr.PlayerGui
    for _, tool in pairs (tools) do
      local currentButton = toolButton:Clone()
      currentButton.Text = tool.Name
      currentButton.Parent = currentGui.MainFrame.Tools
      currentButton.MouseButton1Click:Connect(function()
      if currentButton.BackgroundTransparency == 0.3 then
        table.remove(givingTools, tool)
        currentButton.BackgroundTransparency = 0.8
      else
        table.insert(givingTools, tool)
        currentButton.BackgroundTransparency = 0.3
      end
      end)
    end
    giveButton.MouseButton1Click:Connect(function()
    currentGui:Destroy()
    for _, tool in pairs (givingTools) do
      tool.Parent = replicatedFolder
    end
    givingEvent:FireServer(replicatedFolder, givingPlr)
    end)
  end
  end)
end
Ad

Answer this question