NPC won't drop item if leaderstat is above number

I am new at scripting, so this may be a really simple thing, but I just can’t manage to do it even after a lot of reading on the devforums. What is supposed to happen is when you kill an npc it would drop a tool but only if you’re a certain level. However, instead it does nothing, not giving you the tool either.

Here is what scripts there are so far,
Inside a local script:

lvl = game.Players.LocalPlayer.leaderstats.Level.Value
if lvl >= 10 then
	script.Parent.MobDropScript.CanDropItem.Value = true
end

Inside a normal script:

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy

function DropItem()
	if (Mob == nil) or (Enemy == nil) then return end
	if (not Enemy:FindFirstChild("Player_Tag")) then return end
	local Tag = Enemy:FindFirstChild("Player_Tag")
	if (not Tag) and (not Tag.Value) then return end
	local plr = Tag.Value
	local BP = plr.Backpack
	local SG = plr.StarterGear
	if _M.WeaponDirectory:FindFirstChild(_M.WeaponName) then
		if SG then
			if (SG:FindFirstChild(_M.WeaponName)) then return end
			local W1 = _M.WeaponDirectory[_M.WeaponName]:Clone()
			W1.Parent = SG
		end
		if BP then
			if (BP:FindFirstChild(_M.WeaponName)) then return end
			local W2 = _M.WeaponDirectory[_M.WeaponName]:Clone()
			W2.Parent = BP
		end
	end
end
-- Below does not work.
if _M.DropsWeapon == true then
  if script.CanDropItem.Value == true then
	Enemy.Died:Connect(DropItem)
		
	end
end

And inside the normal script is just a BoolValue named CanDropItem.
I’ve tried many things, deleting the item if not a certain level, needing a level to use the tool, needing a level to obtain the tool, but it just doesn’t work. This is my first post, so if I missed anything please tell me.

CanDropItem.Value will never get set on the server, no matter the players level.

You are setting CanDropItem.Value on the client(in a local script). The Value will not update to the server, and your normal script will never see the changes to the Value. Thanks to FilteringEnabled, you need to make and set the player level checks on the server, or your normal script will not see the changes. For the most part, ServerScripts do not see things that LocalScripts do.

So I just need to find the level of the player inside the normal script? I’m not sure how I would do that either - that was another one of my attempts. How would you get a leaderstat of the player playing inside a normal script?

The “leaderstats” should be on the server too, right? Therefore you should be able to move your localscript code, into a serverscript. The only difference is that you would have to find the player from the character, instead of being able to access it directly through Players.LocalPlayer.

Hey, it didn’t work, or rather, I couldn’t find out how to get the local player without it touching a part or pressing a key… My best attempt was:

local _M = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy
local Tag = Enemy:FindFirstChild("Player_Tag")
if (not Tag) and (not Tag.Value) then return end
local plr = Tag.Value

function DropItem()
	if (Mob == nil) or (Enemy == nil) then return end
	if (not Enemy:FindFirstChild("Player_Tag")) then return end
	local Tag = Enemy:FindFirstChild("Player_Tag")
	if (not Tag) and (not Tag.Value) then return end
	local plr = Tag.Value
	local BP = plr.Backpack
	local SG = plr.StarterGear
	if _M.WeaponDirectory:FindFirstChild(_M.WeaponName) then
		if SG then
			if (SG:FindFirstChild(_M.WeaponName)) then return end
			local W1 = _M.WeaponDirectory[_M.WeaponName]:Clone()
			W1.Parent = SG
		end
		if BP then
			if (BP:FindFirstChild(_M.WeaponName)) then return end
			local W2 = _M.WeaponDirectory[_M.WeaponName]:Clone()
			W2.Parent = BP
		end
	end
end
-- Below does not work.

lvl = plr.leaderstats.Level
if _M.DropsWeapon == true then 
   if lvl >= 0 then
       Enemy.Died:Connect(DropItem)

       end
	end

But the problem might be out of that script. So I am really stuck on what to do. Sorry for bothering you.

I might be kinda of late but, if Enemy is just the Character and isnt the Humanoid then the event wont work.

If it has a humanoid inside you can just do Enemy.Humanoid.Died.

Another thing, lvl = plr.leaderstats.Level.

That thing wont work like that, you would need to add a .Value in it so it would be like:

lvl = plr.leaderstats.Level.Value

You could also do something to detect the changes in the Module Script because doing in that way (if _M.DropsWeapon == true then) will make it so it only checks once.

1 Like