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

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

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

2 comments

0

Can you put the script on the website i rather not click or download files

0

Is this a Script or a LocalScript?

1 answer

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

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