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

Why does my NPC drop script work in studio but not in a server?

I made a script that is supposed to give a player a rare item when the NPC is killed. It works in studio when I test it, but when I fire up a server to test it, it doesn't work. Anyone know why?

script in NPC model:

local Humanoid = script.Parent.Human
local items = game.ReplicatedStorage.Swords
local plr = game.Players.LocalPlayer

function PwntX_X() 
 local tag = Instance.new('ObjectValue',Humanoid)
tag.Name = 'creator'
 tag.Value = plr

    if tag ~= nil then 
        if tag.Value ~= nil then 
            local droprate = math.random(1,1)
            if droprate == 1 then
                print("Bandit LongSword")
                    local tb = items["Bandit LongSword"]:Clone()
                    local tb2 = items["Bandit LongSword"]:Clone()
                    tb.Parent = plr.Backpack
                    tb2.Parent = plr.StarterGear
            end
wait(3)
tag:Destroy()
local Leaderstats = tag.Value:findFirstChild("leaderstats") 
            if Leaderstats ~= nil then 
Leaderstats.Gold.Value = Leaderstats.Gold.Value + 5
wait(0.1)
script:remove()
            end 
        end 
    end 
end 
Humanoid.Died:connect(PwntX_X) 

4 comments

0

line 15 should be StarterGear not StarterPack. Also we need more context as to what "creator" is. Print debug it

0

I'm not sure what the "creator" value is, I can't find it anywhere, but I do know that its supposed to tag the player, I'll rewrite that and see what happens.

0

I rewrote it but I still have the same problem.

0

The creator tag is commonly used in leaderboard/weapon scripts to detect the killer of a character.

1 answer

✓ Accepted answer

Your main mistakes were:

  • LocalPlayer only works in LocalScripts.
  • The tag is meant to be created by the weapon and interpreted by the script that handles awarding kills/gold/exp/etc.
  • You used :remove() not :Destroy() when you wanted to remove the script (:remove() is deprecated, meaning it is not recommended to be used due to it being older and having the possibility of being removed at any time without warning).
  • This would run for every player when the character dies, if this was a LocalScript.

Script inside the NPC:

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid") -- WaitForChild is very useful for preventing the errors caused by things not being loaded in time for the script
local Items = game.ServerStorage:WaitForChild("Swords")

local function DropItems(Player)
	local DiceRoll = math.random(1,100)
	if DiceRoll <= 5 then -- 5% dropchance
		local Sword = Items:WaitForChild("Bandit LongSword")
		Sword:Clone().Parent = Player.Backpack -- this is a very useful shortcut when you don't need to use the clone later
		Sword:Clone().Parent = Player.StarterGear
	end
end

local function GiveGold(Player)
	local Leaderstats = Player:WaitForChild("leaderstats")
	local Gold = Leaderstats:WaitForChild("Gold")
	Gold.Value = Gold.Value + 1
end

Humanoid.Died:connect(function()
	local Tag = Humanoid:FindFirstChild("creator")
	if not Tag or not Tag.Value then return end -- If there is not a tag or player
	DropItems(Tag.Value)
	GiveGold(Tag.Value)
end)

LocalScript inside the weapon:

-- this code will need to be implemented into the weapon scripts
-- note that this doesn't work with filtering enabled

local Tag = Instance.new("ObjectValue")
Tag.Name = "creator"
Tag.Value = game.Players.LocalPlayer
Tag.Parent = TargetNPC:WaitForChild("Humanoid") -- put it inside the humanoid of the damaged humanoid
game.Debris:AddItem(Tag,3) -- adds it to be removed in 3 seconds

Hope I helped! If you have any questions, please ask them :)

~TDP

1 comment

1

Thank you so much!

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