So I am trying to make a loot system for when you kill NPCs but the output keeps saying
"Players.557mastered.Backpack.TrashSword.Handle.Damage:3: attempt to index nil with ‘Character’ " This error is on line three of this script
local Handle = script.Parent
local Player = game.Players:FindFirstChild(Handle.Parent)
local Character = Player.Character
local DamageToProduce = 1
local Debounce = true
local function onTouch(part)
if Debounce then --We check if it can trigger to avoid and limit it from doing infinite constant damage
Debounce = false
local EnemyToHit = part.Parent -- Not called NPC to generalize
local EnemyHumanoid = part.Parent:FindFirstChildOfClass("Humanoid")
if EnemyHumanoid and EnemyToHit ~= Character then --Check if Enemy isn't your character
if (EnemyHumanoid.Health - DamageToProduce) <= 0 then --Check if Damage will kill him or not before doing damage
EnemyHumanoid:TakeDamage(DamageToProduce) --Produce Damage AFTER checking
local PlayerName = Instance.new("StringValue")
PlayerName.Name = Player.Name
PlayerName.Parent = EnemyToHit
else
EnemyHumanoid:TakeDamage(DamageToProduce) --If hit didn't kill then don't create string and just let it take damage
end
end
spawn(function()
wait(0.3)
Debounce = true
end)
end
end
Handle.Touched:Connect(onTouch())
Here is the script i am trying to use it for
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Tools = game.ReplicatedStorage.Tools
local LootPlan = require(ReplicatedStorage.LootPlan)
local DropPlan = LootPlan.new("single")
DropPlan:AddLoot("Apple of Healing", 99)
DropPlan:AddLoot("Nothing", 1)
local Humanoid = script.Parent.Humanoid
function Dead()
local Backpack = Players.Player.Backpack
local dropType = DropPlan:GetRandomLoot()
local getPlayer = script.Parent:FindFirstChildOfClass("StringValue") -- Checks if there is a value
if getPlayer then
if dropType ~= "Nothing" then
Tools[dropType]:Clone().Parent = Players[getPlayer.Name].Backpack
end
end
end