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

Why won't my stats increase when I get kill an NPC?

This script is bond to a sword as the parent

local hit = script.Parent.Handle.Touched
local player = game.Players.LocalPlayer
local stats = player:WaitForChild("Money")
local sp = stats:WaitForChild("Money")
if hit.parent.Humanoid.Health <= 0 then
	sp.value = sp.value + 300 
end

Changed it but i said i wanted to get a stat increase from an NPC not you dieing

local Handle = script.Parent.Handle
Handle.Touched:Connect(function(part)
   local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
   if player then
      local stats = player:WaitForChild("Stats")
      local sp = stats:WaitForChild("Money")
      local zombie = part.Parent:findfirstchild("Zombie")
      local humanoid = zombie.humanoid
      if humanoid.Health <= 0 then
         sp.Value = sp.Value + 300
      end
   end
end)

2 answers

✓ Accepted answer

The points need to be handled on the server, otherwise the change won't replicate. Thankfully, the entire script can be done server-side:

local Handle = script.Parent.Handle

Handle.Touched:Connect(function(part)
   local player = game:GetService("Players"):GetPlayerFromCharacter(part.Parent)
   if player then
      local stats = player:WaitForChild("Money")
      local sp = stats:WaitForChild("Money")
      local humanoid = part.Parent.Humanoid
      if humanoid.Health <= 0 then
         sp.Value = sp.Value + 300
      end
   end
end)

#Accept and upvote if this helps!

2 comments

0

Wait a second why does it increase so much when i die? Instead of 300 it gave me 3000

0

you might have to add a debounce

Here's a .Touched event example.

local function onTouched(hit)
	--your code
end
script.Parent.Touched:Connect(onTouched)

Also, game.Players.LocalPlayer can only be used in a local script and leaderstat values can only be changed on server-side scripts.

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