How Could I Make A Player Drop This Orb

So basically, I have a script which drops these tiny orbs of exp to the ground when an npc is killed, it works great and all but I’m trying to find out how I can make it so that a player can drop the orb on death like how the npc does.

Currently I tried to put the code into a .Died event but it didn’t work, there were no error but the script just didn’t work.

Here is the code so the player can collect the orb:

	if hit.Parent:FindFirstChild("Humanoid") then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if hit.Parent.Humanoid.Health > 0 then
			player.leaderstats.Levels.Value = player.leaderstats.Levels.Value + 1
			script.Parent:Destroy()
		end
	end
end)

Here is the code so the npc can drop the orb:

	local Levels = game.ReplicatedStorage.Levels:Clone()
	Levels.Parent = game.Workspace
	Levels.Position = game.Workspace.Npc.HumanoidRootPart.Position
end)

Also, this is my first ever post so sorry if everything isn’t perfect/

Answers in code snippets would be highly appreciated. :grinning:

This is a ServerScript in ServerScriptService.

1 Like

Question. Where did you put the died event at?

1 Like

I put it inside a player.CharacterAdded

1 Like

The code you put inside of there was a local script, correct?

No, both of them are ServerScripts for the npc.

The one with the playerdied must be a local script for it to work

Ok, I ill try a local script and see if it works.

Question, will it be in the StarterCharacterScripts as a local script?

Yes or starterplayerscripts whichever is fine as long as you edit it properly

I still experienced the same thing, no error but the orb wont clone.

Couldn’t you just do this inside ServerScriptService? That’ll both be replicated across the server & client alone for the orb to collect:

game.Players.PlayerAdded:Connect(function(Player)
    Player.CharacterAdded:Connect(function(Character)
        local Humanoid = Character:WaitForChild("Humanoid")
        local HRP = Character:WaitForChild("HumanoidRootPart")

        Humanoid.Died:Connect(function()
            local Levels = game.ReplicatedStorage.Levels:Clone()
            Levels.Parent = workspace
            Levels.Position = HRP.Position + Vector3.new(math.random(-5, 5),0, math.random(-5, 5))
        end)
    end)
end)

Unless if I’m misunderstanding the OP here

Ok, thank you. Let me try the code out and see if it works.

Do make sure that the Script is a ServerScript btw

Thank you so much, this resolves the issue. Turns out I wasn’t defining the HumanoidRootPart correctly.

1 Like

Ah not to worry you’re fine! Small mistakes like those can happen :sweat_smile: