I’ve been working on this RP for over a year, and it’s being held up by a particular roadblock; the item giver does nothing. It started out with me parenting the tool to the character, and the item appearing in the player’s hotbar, but the tool itself just falls to the ground from where the original item was.
I tried to fix it by parenting the tool to the player’s backpack, and it stopped working entirely. I’m not saying it gave an error, I’m saying it did absolutely nothing. No tool spawned, nothing appeared in the player’s backpack, and nothing was displayed in the terminal.
Here’s the script
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
local player = part.Parent:FindFirstChild("Humanoid")
local backpack = part.Parent:FindFirstChild("Backpack")
if player and not player.Parent:FindFirstChild("Spear", true) then
local item = givenItem:Clone()
local itemChildren = givenItem:GetChildren()
local tool = Instance.new("Tool")
tool.Parent = backpack
tool.Name = "flint spear"
item.Parent = tool
itemChildren.Parent = item
end
end
trigger.Touched:Connect(giveItem)
Youre attempting to find the backpack inside the character which is actually parented to the player instance.
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
local Character = part.Parent
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid and not Character:FindFirstChild("Spear") then
local Backpack = game:GetService("Players"):GetPlayerFromCharacter(Character).Backpack
if (Backpack:FindFirstChild("Spear")) then return end --// Checks if the player already has a spear inside his backpack.
local Item = givenItem:Clone()
Item.Name = "flint spear"
Item.Parent = Backpack
end
end
trigger.Touched:Connect(giveItem)
I think your script also has some errors. This should be the fixed version:
local Trigger = script.Parent.ForceField
local GivenItem = script.Parent.Spear
function giveItem(Hit)
if hit.Parent:FindFirstChild("Humanoid") then --Checks if Parent is a Player
local Player = game.Players:FindFirstChild(Hit.Parent.Name)
local Backpack = Player.Backpack
if not Backpack:FindFirstChild("Spear") or Player.Character:FindFirstChild("Spear") then --Checks if the players already has the Spear
local Tool = GivenItem:Clone()
Tool.Parent = backpack
tool.Name = "flint spear"
end
end
end
Trigger.Touched:Connect(Hit)
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
local player = part.Parent:FindFirstChild("Humanoid")
local backpack = game:GetService("Players"):GetPlayerFromCharacter(part.Parent).Backpack
if player and not backpack:FindFirstChild("Spear") then
local item = givenItem:Clone()
local itemChildren = givenItem:GetChildren()
local tool = Instance.new("Tool")
tool.Parent = backpack
tool.Name = "flint spear"
item.Parent = tool
itemChildren.Parent = item
end
end
trigger.Touched:Connect(giveItem)
and it gave me the error “attempt to index nil with ‘Backpack’”
The reason you’re getting this error is probably because the part being touched isn’t parented to a character or because :GetPlayerFromCharacter() is having issues. To check which is the case, it makes sense to make game:GetService("Players"):GetPlayerFromCharacter(part.Parent) its own variable. Here’s an example:
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
print(part.Name)
local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
if not player then warn("No player found.") return end
local backpack = player:FindFirstChild("Backpack")
if backpack:FindFirstChild("Spear") then warn("Spear already in inventory.") return end
local item = givenItem:Clone()
local itemChildren = givenItem:GetChildren()
local tool = Instance.new("Tool")
tool.Parent = backpack
tool.Name = "flint spear"
item.Parent = tool
itemChildren.Parent = item
end
trigger.Touched:Connect(giveItem)
You don’t need to check for a humanoid if you’re checking for a character’s corresponding player and aren’t going to be referencing the humanoid later in the code. You can just check for the player and it will still work the same.
No, because :GetPlayerFromCharacter() is a function that returns either a player or nil. If it returned nil, you wouldn’t know unless you told the script to tell you (via print()) or you try to attempt to index it with something else, like you’re doing when you search for .Backpack.
If it errored every time it couldn’t find a player value, then calling the function at all would risk breaking your script unless you were 100% sure that the model you’re feeding it was going to be a character model, every time. Roblox has explicitly written the function to make sure you don’t need to worry about it.
If that’s the case, what should I change in this script?
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
local backpack = player.Backpack
if player and not backpack:FindFirstChild("Spear") then
local item = givenItem:Clone()
if not player then warn("no player found") return end
local itemChildren = givenItem:GetChildren()
if backpack:FindFirstChild("Spear") then warn("spear already acquired") return end
local tool = Instance.new("Tool")
tool.Parent = backpack
tool.Name = "flint spear"
item.Parent = tool
itemChildren.Parent = item
end
end
trigger.Touched:Connect(giveItem)
Are you doing this in a server script or local script?
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
local player = part.Parent:FindFirstChild("Humanoid")
local backpack = part.Parent:FindFirstChild("Backpack")
if player and not player.Parent:FindFirstChild("Spear", true) then
local item = givenItem:Clone()
local itemChildren = givenItem:GetChildren()
local tool = Instance.new("Tool")
tool.Parent = backpack
tool.Name = "flint spear"
item.Parent = tool
itemChildren.Parent = item
end
end
trigger.Touched:Connect(giveItem)
After looking at this line here, this may be the cause of the issue since you’re trying to find the players backpack in the character which doesn’t exist and causes the item giver to do nothing.
local Players = game:GetService("Players")
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
local player = part.Parent:FindFirstChild("Humanoid")
local backpack = Players:FindFirstChild(part.Parent.Name):WaitForChild("Backpack")
if player and not player.Parent:FindFirstChild("Spear", true) then
local item = givenItem:Clone()
local itemChildren = givenItem:GetChildren()
local tool = Instance.new("Tool")
tool.Parent = backpack
tool.Name = "flint spear"
item.Parent = tool
itemChildren.Parent = item
end
end
trigger.Touched:Connect(giveItem)
local Players = game:GetService("Players")
local trigger = script.Parent.ForceField
local givenItem = script.Parent.Spear
function giveItem(part)
local Player = Players:GetPlayerFromCharacter(part.Parent)
if Player ~= nil then
local Him = Player.Character:FindFirstChildOfClass("Humanoid")
Him:UnequipTools()
if not Player.Backpack:FindFirstChild("Spear") then
local Tool = givenItem:Clone()
Tool.Name = "flint spear"
Tool.Parent = Player.Backpack
end
end
end
trigger.Touched:Connect(giveItem)
Three attempts at rearrangements of your code, either gave ‘attempt to index nil with “WaitForChild”’, ‘attempt to index nil with “FindFirstChild”’ or ‘attempt to index nil with “Backpack”’
Remove that first conditional, if player and not backpack:FindFirstChild("Spear") then. Your code is already checking for those conditions after the condition passes and assigning the backpack variable to player.Backpack before you’ve verified that player isn’t nil has the possibility of breaking your script.