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

I have an error that says "Attemp to index nil with "Name" in the line code 21" help me please?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local PickupItem = ReplicatedStorage:WaitForChild("PickupItem")

local PickupTweenInfo = TweenInfo.new(
    .3,
    Enum.EasingStyle.Linear,
    Enum.EasingDirection.In,
    0,
    false,
    0
)

local MaxPickUpDistance = 30

PickupItem.OnServerEvent:Connect(function(player, item)
    local char = player.Character
    local Inventory = player.Inventory
    
    local itemValue = Inventory:FindFirstChild(item.Name)
    if itemValue then
        itemValue.Value = itemValue.Value + 1
        local finishingLocation = char.HumanoidRootPart.CFrame
        item.CanCollide = false
        item.Anchored = true
        local tween = TweenService:Create(item,PickupTweenInfo, {Transparency = 1, Size = Vector3.new(0,0,0), CFrame = finishingLocation})
        tween:Play()
    end
end)

> 

The local script that fire the remote event local UIS = game:GetService("UserInputService") local pickupKey = "F" local ReplicatedStorage = game:GetService("ReplicatedStorage")

local player = game.Players.LocalPlayer local mouse = player:GetMouse()

local PlayerGui = player:WaitForChild("PlayerGui") local PickupInfoGui = PlayerGui:WaitForChild("PickupInfoGui") local PickupItem = ReplicatedStorage:WaitForChild("PickupItem")

UIS.InputChanged:Connect(function(input) if mouse.Target then if mouse.Target:FindFirstChild("Pickable") then local item = mouse.Target PickupInfoGui.Adornee = item PickupInfoGui.ObjectName.Text = item.Name PickupInfoGui.Enabled = true else PickupInfoGui.Adornee = nil PickupInfoGui.Enabled = false end end end)

UIS.InputEnded:Connect(function(input) if input.KeyCode == Enum.KeyCode[pickupKey] then if mouse.Target then if mouse.Target:FindFirstChild("Pickable") then local item = mouse.Target if item then local DistanceFromItem = player:DistanceFromCharacter(item.Position) if DistanceFromItem < 30 then PickupItem:FireServer() end end end
end end end)

This local script works perfectly




9 comments

0

I think 'item' is the name. You don't need item.Name

0

"item" would need to be defined for you to get it via name, but if you already have it defined why are you using Inventory:FindFirstChild(item.Name) when you could do itemValue = item.Name? Also, try printing item to see what it returns and mess around with where item is being sent from until print actually returns the instance.

0

Can you give the code that fires PickupItem? When it says "Attempt to index nil with 'Name'" it means that the variable item, the thing you are trying to access the property "Name" is nil. Perhaps you're not passing the variable correctly on the other script?

0

" item" is the workspace item

0

that i pick up, so i put name that can be "Log", etc

0

file:///C:/Users/Usuario/Pictures/Screenshots/Captura%20de%20pantalla%20(128).png

0

i put an image to specified

0

You forgot to pass the item first: PickupItem:FireServer(item)

0

Thanks the last answers works, thanks.

1 answer

✓ Accepted answer

Try this

local UIS = game:GetService("UserInputService")
local replicatedStorage = game:GetService("ReplicatedStorage")
local PickupItem = replicatedStorage:WaitForChild("PickupItem")
local pickupKey = "F"

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()

local PlayerGui = player:WaitForChild("PlayerGui")
local PickUpInfoGui = PlayerGui:WaitForChild("PickUpInfoGui")

UIS.InputChanged:Connect(function(input)
	if mouse.Target then
		if mouse.Target:FindFirstChild("Pickable") then
			local item = mouse.Target
			PickUpInfoGui.Adornee = item
			PickUpInfoGui.ObjectName.Text = item.Name
			PickUpInfoGui.Enabled = true
		else
			PickUpInfoGui.Adornee = nil
			PickUpInfoGui.Enabled = false
		end
	end
end)


UIS.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode[pickupKey] then
		if mouse.Target then
			if mouse.Target:FindFirstChild("Pickable") then
				local item = mouse.Target
				if item then
					local distanceFromItem = player:DistanceFromCharacter(item.Position)
					if distanceFromItem < PickUpInfoGui.MaxDistance then
						-- Grab it
						PickupItem:FireServer(item)
					end
				end
			end
		end
	end
end)

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