Cash On Kill Script Not Working

I’m trying to make a cash on kill script. I don’t know why this isn’t working, no errors are shown in the output.

local data_store_name = "ICE" 
local ds = game:GetService("DataStoreService"):GetDataStore(data_store_name)

local function join(plr)


    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr
    
    local gold = Instance.new("NumberValue")
    gold.Name = "Gold"
    gold.Value = 0
    gold.Parent = leaderstats
    
    local elixir = Instance.new("NumberValue")
    elixir.Name = "Elixir"
    elixir.Value = 0
    elixir.Parent = leaderstats

    local darkElixir = Instance.new("NumberValue")
    darkElixir.Name = "Dark Elixir"
    darkElixir.Value = 0
	 darkElixir.Parent = leaderstats
	-----------------------------------
	
	 plr.CharacterAdded:Connect(function(character)
			character.Humanoid.Died:Connect(function()
				local tag = character.Humanoid:FindFirstChild("creator")
				if tag ~= nil then
					local player = tag.Value
					local bounty = 20
				
					local leaderstats = player:WaitForChild("leaderstats")
					leaderstats.Gold.Value = leaderstats.Gold.Value + bounty
				end
			end)
	end)
	
    
    -- Add this part no mater what!
    local data1 
    local data2
    local data3

    local suc, err = pcall(function()
      -- you can add more if you ever want to
      data1 = ds:GetAsync(plr.UserId.."_gold")
      data2 = ds:GetAsync(plr.UserId.."_elixir")
      data3 = ds:GetAsync(plr.UserId.."_darkElixir")
  end)

  if suc then
      plr.leaderstats.Gold.Value = data1
      plr.leaderstats.Elixir.Value = data2
      plr.leaderstats:FindFirstChild("Dark Elixir").Value = data3
  else
    warn("Couldn't get ".. plr.Name .. "'s data.\n", err)
  end
end

local function leave(plr)
  local suc, err = pcall(function()
    -- If you added any extra values at top, remember to add them here!
    ds:SetAsync(plr.UserId.."_gold", plr.leaderstats.Gold.Value)
    ds:SetAsync(plr.UserId.."_elixir", plr.leaderstats.Elixir.Value)
    ds:SetAsync(plr.UserId.."_darkElixir", plr.leaderstats:FindFirstChild("Dark Elixir").Value)
  end)

  if not suc then
    warn("Couldn't save ".. plr.Name.."'s data.\n".. err)
  end
end

game.Players.PlayerAdded:Connect(join)
game.Players.PlayerRemoving:Connect(leave)

Thanks

3 Likes

So is the goal here to award someone 20 gold when they kill another player?

yeah

charrrrrrrrrrrrrrrrrrrrrrr

1 Like

I don’t get where you are actually creating the datastore. It looks like you are saving data to the datastoreservice which doesn’t work. You need to say:

local datastore = DataStoreService:GetDataStore(“MyDataStore ”)

Then u can save ur stuff to there.

Just curious… Did you copy this from someone? Because it probably doesn’t work if you don’t know how it works.

1 Like

Not a helpful reply.

Have you tried adding prints everywhere to see what doesn’t get printed (aka debugging)?

1 Like