How to make a npc give you a tool when you kill them

I am trying to make a system like Sword Fighting Simulator where you kill a NPC and get a sword. I have done most of the work but the sword does not work after being cloned.

This is in the NPC Humanoid ( Normal Script)

script.Parent.Died:Connect(function()
	if deb == false then
		deb = true
		game.ReplicatedStorage.Remote.DeathEvent.AEvent:FireAllClients()
		wait(1)
		deb = false
	end
end)

This is in StarterGui as a LocalScript

	local player = game.Players.LocalPlayer
	local Sword = game.ReplicatedStorage.Swords.A:Clone()
	Sword.Parent = player.Backpack
	player.leaderstats.Parts.Value += 100
end)

I have a RemoteEvent in ReplicatedStorage

What did I do wrong???

5 Likes

I would just like to mention that there is no need to have the cloning process of the sword & the editing of a leaderstat value happen under the client side (local script) as those can be done completely off the server side (Normal Script)

Having a simple for loop that does “for _,v in pairs (game.Players:GetPlayers()) do” and in that for loop you give each player, aka v, a sword “Sword.Parent = v.Backpack” and have the leaderstat be altered there “v.leaderstats.Parts.Value += 100” would be somewhat more secure, and may even save u a remote event.

3 Likes

Where do I put the script. I am wanting to learn so can you tell me why the normal script works or is better then the local script

1 Like

A bit off topic but I would put this into Help and Feedback - Scripting Support
Also when you’re sharing your code, put it in between 3 tildas like this:

` is a tilda btw

local deb = false
script.Parent.Died:Connect(function()
	if deb == false then
		deb = true
		game.ReplicatedStorage.Remote.DeathEvent.AEvent:FireAllClients()
		wait(1)
		deb = false
	end
end)
game.ReplicatedStorage.Remote.DeathEvent.AEvent.OnClientEvent:Connect(function()
	local player = game.Players.LocalPlayer
	local Sword = game.ReplicatedStorage.Swords.A:Clone()
	Sword.Parent = player.Backpack
	player.leaderstats.Parts.Value += 100
end)
1 Like

To hopefully fix your problem,
In the main script, I changed the code to this:

local deb = false
local RS = game.ReplicatedStorage

script.Parent.Humanoid.Died:Connect(function()
	if not deb then -- if the debounce is false, then
		deb = true -- make it true
		local SwordClone = RS.Swords.A:Clone() -- clones a sword from replicated storage
		
		for _, v in pairs(game:GetService("Players"):GetPlayers()) do -- we use a for loop to get every player in the server
			v.leaderstats.Parts.Value = v.leaderstats.Parts.Value + 100 -- adds "100" to their Parts
			SwordClone.Parent = v.Backpack -- moves the Sword to their backpack
		end
		
		task.wait(1) -- task.wait() is more efficient than wait()
		deb = false -- ends the debounce
	end
end)

Other notes:

  • In your local script you could’ve put it inside of StarterPlayerScripts, but there’s no need to really. We can handle everything on the server.

  • If you used your code, the player would get the tool/100 parts locally, and the server/other players wouldn’t notice the change, meaning it would be as if it never existed.

  • I removed the local script and the RemoteEvent since there’s no need for them.

  • Use Code Review as a place to improve code, and use Scripting Support to fix your code

  • Have a great day!

Holy Cow that Worked Thank you SO MUCH!!! and thanks for emplaning it!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.